Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I am stuck with a problem to link static libraries with gcc. There is no problem with source files since I am able to compile in a machine where ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    17

    Post Prob with linking in gcc!


    Hello,

    I am stuck with a problem to link static libraries with gcc.
    There is no problem with source files since I am able to compile in a machine where the static library is installed.
    I am compiling with the following:
    Code:
    :~/Emotion/pjproject-1.0.3/third_party 157% gcc -Wall -I/portaudio/include -o rec patest_record.c -L./lib -lportaudio-x86_64-unknown-linux-gnu
    
    My current path is: ~/Emotion/pjproject-1.0.3/third_party 
    Required header file path is: ~/Emotion/pjproject-1.0.3/third_party/portaudio/include/portaudio.h
    Static Library path is: ~/Emotion/pjproject-1.0.3/third_party/lib/libportaudio-x86_64-unknown-linux-gnu.a
    I don't know where I am committing mistake but I get a loads of undefined reference errors.
    I am producing the end part of them:
    ************************************************** ************************************************** ****
    Code:
    pa_unix_oss.c:(.text+0x1d60): undefined reference to `__pthread_unregister_cancel'
    ./lib/libportaudio-x86_64-unknown-linux-gnu.a(pa_unix_oss.o): In function `PaOssStream_Terminate':
    pa_unix_oss.c:(.text+0x1fb4): undefined reference to `sem_destroy'
    ./lib/libportaudio-x86_64-unknown-linux-gnu.a(pa_unix_oss.o): In function `OpenStream':
    pa_unix_oss.c:(.text+0x2bc6): undefined reference to `sem_init'
    pa_unix_oss.c:(.text+0x2d45): undefined reference to `ceil'
    collect2: ld returned 1 exit status
    ************************************************** ************************************************** ***

    Pls help me to resove this issue!
    Thanks,
    Abhishek
    Last edited by MikeTbob; 10-17-2010 at 12:19 AM. Reason: Added Code tags

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    PortAudio also needs to be linked against a pthread library (with -lpthread or just -pthread).

    Ideally, you should find the portaudio.pc file (in ${prefix}/lib/pkgconfig/portaudio.pc, where ${prefix} is whatever prefix you passed to the configure script). Add something like:

    Code:
    `pkg-config /path/to/portaudio/install/lib/pkgconfig/portaudio.pc --libs`
    To your command-line when you link and it should give you all the dependencies it needs.

    (You can also use --cflags instead of --libs to get the options it wants for the compiler.)

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    17

    Post Linking problem in gcc!

    Thanks for your reply. I understand that it can be done through pkg-config.
    But, I have another query.
    Using the following :
    ************************************************** *************************
    Code:
    abhat002@shetland:~/third_party 167% gcc -Wall -I/portaudio/include/ -o
    rec2 patest_record.c lib/libportaudio-x86_64-unknown-linux-gnu.a -lpthread
    -lrt -lasound -lm
    ************************************************** *************************
    compiles fine and the binary runs perfectly.Whereas the following:
    ************************************************** *************************
    Code:
    abhat002@shetland:~/third_party 169% gcc -Wall -I/portaudio/include/ -o
    rec2 patest_record.c -L./lib -lportaudio-x86_64-unknown-linux-gnu
    -lpthread -lrt -lasound -lm
    ./lib/libasound.a(dlmisc.o): In function `snd_dlsym_verify':
    (.text+0x1df): undefined reference to `dlsym'
    ./lib/libasound.a(dlmisc.o): In function `snd_dlsym':
    (.text+0x257): undefined reference to `dlsym'
    ./lib/libasound.a(dlmisc.o): In function `snd_dlclose':
    (.text+0x2ba): undefined reference to `dlclose'
    ./lib/libasound.a(dlmisc.o): In function `snd_dlopen':
    (.text+0x346): undefined reference to `dlopen'
    collect2: ld returned 1 exit status
    abhat002@shetland:~/third_party 170%
    ************************************************** *************************
    The only difference in the second is in specifying path through -L flag
    and so why does it fail to compile though I am using the same static
    libraries!
    I am not very clear in understanding the exact linking process in gcc.

    Thanks!
    Last edited by MikeTbob; 10-17-2010 at 12:19 AM. Reason: Added Code tags

  4. #4
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by abhishek2301 View Post
    abhat002@shetland:~/third_party 167% gcc -Wall -I/portaudio/include/ -o
    rec2 patest_record.c lib/libportaudio-x86_64-unknown-linux-gnu.a -lpthread
    -lrt -lasound -lm

    abhat002@shetland:~/third_party 169% gcc -Wall -I/portaudio/include/ -o
    rec2 patest_record.c -L./lib -lportaudio-x86_64-unknown-linux-gnu
    -lpthread -lrt -lasound -lm
    The difference is that in the first one, you're explicitly telling the compiler to link-in lib/libportaudio-x86_64-unknown-linux-gnu.a. In the second one, you're using -L./lib to tell it to look in ./lib for libraries, and -lportaudio-x86_64-unknown-linux-gnu to tell it to look for portaudio-x86_64-unknown-linux-gnu somehow.

    In the second case, it has probably spotted (correct me if I'm wrong) a file called ./lib/libportaudio-x86_64-unknown-linux-gnu.so, which is a shared object library, as well as ./lib/libportaudio-x86_64-unknown-linux-gnu.a, a static library. All other things being equal, the linker will prefer to use the shared library (since this is usually what you want) and this is causing your problem.

    If you want to link to the shared library, tagging -ldl to the end of your command should fix it (or at least it should fix that problem).

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    17

    Post gcc linking problem!

    Thanks for your reply!
    But there are no files /lib/libportaudio-x86_64-unknown-linux-gnu.so and only *.a file is present in the lib folder. So, there is no chance of linking with a dynamic library.
    I also tried with the -static option so that it takes only *.a files but still the same undefined reference errors.
    The linking process seems to be very puzzling to me and I could not find any good resource on the web!

    Thanks!

  6. #6
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by abhishek2301 View Post
    But there are no files /lib/libportaudio-x86_64-unknown-linux-gnu.so and only *.a file is present in the lib folder.
    Okay - it could be somewhere else on your system. What's the output of doing:

    Code:
    sudo updatedb
    locate libportaudio-x86_64-unknown-linux-gnu
    In a bash shell?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •