We help IT Professionals succeed at work.

Compiling C prog in Linux can not find function reference in library

UnderSeven asked
Medium Priority
Last Modified: 2012-08-13
I'm attempting to compile a program in linux using C.  my linux version is

This is the command I am using to compile:

gcc test2.c /usr/local/lib/libtdsodbc.a

As you can see I have a library in /usr/local/lib that this program requires.  I do not have my instance set up to look in there automatically so I have to tell it to find the library there.  This works part way, but then one of the functions invoked within the library itself seems to then run into a similar problem, I get this error:

test2.c: In function âmainâ:
test2.c:18: warning: incompatible implicit declaration of built-in function âmallocâ
/usr/local/lib/libtdsodbc.a(connectparams.o): In function `myGetPrivateProfileString':
/home/username/freetds-0.92.dev.20110819/src/odbc/connectparams.c:143: undefined reference to `SQLGetPrivateProfileString'
collect2: ld returned 1 exit status

The function it is trying to find is infact within the very library I am already telling it where to find.  So in some round about way, something invoked by code related to the libraries I'm including are trying to then look at libraries that it can not find, as demonstrated by this grep:

grep SQLGetPrivateProfileString /usr/local/lib/*.*
Binary file /usr/local/lib/libodbcinst.so matches
Binary file /usr/local/lib/libodbcinst.so.1 matches
Binary file /usr/local/lib/libodbcinst.so.1.0.0 matches
Binary file /usr/local/lib/libodbc.so matches
Binary file /usr/local/lib/libodbc.so.1 matches
Binary file /usr/local/lib/libodbc.so.1.0.0 matches
Binary file /usr/local/lib/libtdsodbc.a matches
Binary file /usr/local/lib/libtdsodbc.so matches
Binary file /usr/local/lib/libtdsodbc.so.0 matches
Binary file /usr/local/lib/libtdsodbc.so.0.0.0 matches

As you can see, the function is in libtdsodbc.a, which I'm already pointing it to.

I need to know how to make this program compile.  I assume this is an issue with proper library paths.

Watch Question

Try this
gcc -o test2 test2.c -ltdsodbc -L/usr/local/lib

Open in new window

I believe xterm's solution will work.  I'd like to explain why I think xterm's solution will work and your solution failed (in hopes that you are wondering why).

You asked for everything in libtdsodbc.a to be included in the executable.  Chances are that you don't need (or want) everything in that library.  Using xterm's solution, you will only get what is needed to resolve any unresolved symbols.  You won't get extra stuff that causes the compiler to break.

(Hope I got this right).

Your close. its because UnderSeven didn't tell the compiler that /usr/local/lib/libtdsodbc.a is a library. Also xterms solution dynamically links the two object together, your thinking of static linking.


The explanation was actually helpful.  Of course the solution worked and I'm happy to come away with a little more knowledge of this OS and how it compiles code.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.