Only when I'm trying to compile + link to the pthread library, I'm getting this error from Emacs:

-*- mode: compilation; default-directory: "~/GITHUB/kandr/" -*-
Compilation started at Mon Nov 14 02:50:59

cc -Wall -Wextra -pedantic -std=c11 -g -lpthread how_to_thread.c -o ./bin/how_to_thread
/usr/bin/ld: ne peut ouvrir le fichier de sortie ./bin/how_to_thread: Aucun fichier ou dossier de ce type
collect2: error: ld returned 1 exit status

Compilation exited abnormally with code 1 at Mon Nov 14 02:50:59

I tried to type the same command: cc -Wall -Wextra -pedantic -std=c11 -g -lpthread how_to_thread.c -o ./bin/how_to_thread directly from the command line, and I have no error, the compilation went fine.

The problem seems to occur only when I need to link (-lpthread). Compiling from Emacs a simple c source code, wich doesn't need a library to be linked brings not compilation errors. Is there something I'm doing wrong?

  • Are you running this command in the same directory (~/GITHUB/kandr)? My schoolboy french says this is saying it can't open bin/how_to_thread, which may be due to there being no ~/GITHUB/krandr/bin directory – icarus Nov 14 '16 at 4:41
  • @icarus I'm running everything from ~/GITHUB/kandr directory. The ~/GITHUB/kandr/bin folder exists. Typed from the command line, everything works as expected, but from within Emacs, there is this error. – Nsukami _ Nov 14 '16 at 10:45
  • Can you put everything after the cc in a file, say buildcommand, and then run cc @buildcommand from both inside and outside emacs to eliminate transcription errors. Then add a -### or -v to the commands and compare the outputs to see if different versions are being invoked? Does adding a -lc to a simple c source code compile cause it to break? – icarus Nov 14 '16 at 16:01
  • Do you run emacs from the same terminal where you type the compile command? It could be that emacs (and so the compile command from it) isn't running in the same environment than the shell. – JeanPierre Nov 14 '16 at 20:31

Not sure if this is the only problem, however:

in general, command line parameters are processed in the order listed on the command line.

the -lpthread is listed before the source file and output file so the linker processes the pthread library before processing the object file.

The linker only picks up an entry from a library if it resolves an external reference. But since the library was processed first, there is no external reference to resolve. (it does not have any unresolved external reference until it processes the object file.)

The fix is simple: place the library after the object file (preferably at the end of the command line) similar to:

cc -Wall -Wextra -pedantic -std=c11 -g how_to_thread.c -o ./bin/how_to_thread -lpthread

to make this more obvious and as a general good practice. separate the compile step from the link step, similar to:

cc -Wall -Wextra -pedantic -std=c11 -g -c how_to_thread.c -o how_to_thread.o
cc -g how_to_thread.o -o ./bin/how_to_thread -lpthread
  • How is this related to the commands being in a shell vs in emacs? – JeanPierre Nov 14 '16 at 20:29
  • @user3629249 For a reason I still don't understand, placing the -lpthread at the end of the command was the fix, Emacs is no more complaining and the compilation went fine. That is a little bit strange because, the command: cc -Wall -Wextra -pedantic -std=c11 -g -lpthread how_to_thread.c -o ./bin/how_to_thread was working nice when typed directly from the command line. Can you please elaborate a little bit more on that? Thanks a lot. – Nsukami _ Nov 15 '16 at 10:49
  • All I know is that the linker handles command line parameters from left to right (and ignores all command line parameters that are only for compiling.) As I previously stated, putting the library before the object files results in there being no external links to resolve when processing the library. That is the reason the libraries are listed last – user3629249 Nov 16 '16 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.