Mr. Shickadance
Mr. Shickadance

Reputation: 5473

G++ can't link in some libraries, but using ld separately it finds libraries

On a Ubuntu Linux machine g++ doesn't appear to be linking libraries as it should. I can't supply full source code, but essentially it is a small program which creates a pthread. It compiles fine on two other Debian machines, but on the Ubuntu machine it complains about an undefined reference to pthread_create.

The command line is something like this:

g++ -I. -lpthread source_code.cpp -o program

To debug this I ran g++ under strace to see how it was looking for libpthread. When I did that, I did not see ANY references to libpthread. It was as if the linker wasn't even trying to search for the library. On the machines that did compile I saw several calls to open() when it searched the filesystem for the library.

When I ran ld separately it seems to find libpthread without a problem, and the strace output confirms that it is searching for the library as it should. I ran ld separately like this:

user@machine:~/src$ ld -lpthread
ld: warning: cannot find entry symbol _start; not setting start address

Why could cause g++ to not search the filesystem correctly for libraries when ld seems to do so just fine by itself? Kinda stumped on this one at the moment.

Thanks!

Upvotes: 0

Views: 387

Answers (1)

Slava
Slava

Reputation: 44238

For enabling multithreading in gcc you should pass -pthread flag not -lpthread -

-pthread

       Add support for multithreading using the POSIX threads library.                
       This option sets flags for both
       the preprocessor and linker.  
       It does not affect the thread safety of object code produced by the
       compiler or that of libraries supplied with it.

Upvotes: 1

Related Questions