naruto
naruto

Reputation: 25

Linking libraries in GCC

there are some files that I am trying to compile in ubuntu using makefile. I have added the following lines in my makefile after several searches on web.

run: hellocode.cpp    
    g++ -c hellocode.cpp -lssl -lcrypto

Still while compiling it creates the object files and then throws this error: undefined reference to 'SSL_write'....

on the contrary if remove the '-c' and use it like this

run: hellocode.cpp         
    g++ hellocode.cpp -lssl -lcrypto

Then I dont see the previous errors of linking but it shows different errors not related to openssl linking but related to other files in the code. I have already browsed through many questions on this forum related to this none seem to have helped me in this.

Kindly tell me whether my makefile is wrong or is there some problem with my machine that its not able to link to my library.

Upvotes: 0

Views: 107

Answers (2)

ap-osd
ap-osd

Reputation: 2834

Here's a simple Makefile that you could adopt. Note that compilation and linking are 2 steps. If needed you can use -I for additional include paths and -L for additional link paths.

.PHONY : all 
all : hellocode

hellocode : hellocode.o 
    g++ -o hellocode hellocode.o -lssl -lcrypto

hellocode.o : hellocode.cpp
    g++ -c hellocode.cpp -o hellocode.o

Here are some basics of makefiles if it helps.

Upvotes: 1

Hcorg
Hcorg

Reputation: 12178

library linking should be done at final stage - linking :)

-c means "compile only" - it just builds .o object file, without any reference resolution (so -lXXX is just ignored there).

-lXXX options should be added to last call to gcc (without -c) which produces executable, where all .o files are gathered to link together with libraries to resolve all references.

Upvotes: 0

Related Questions