I am new to makefiles, I want to make an.so library and use it in main.o file, I want to use the library from the current directory. how can I make this works?
CFLAGS = -Wall -g
CC = g++
all: main.o mylib.so
$(CC) main.o mylib.so -o runProg
mylib.so : b1.o a1.o head1.h header2.h header3.h
$(CC) $(CFLAGS) -shared -fPIC -o mylib.so b1.o a1.o
b1.o: b1.cc b1.h
$(CC) $(CFLAGS) -fPIC b1.cc
a1.o: a1.cc
$(CC) $(CFLAGS) -fPIC a1.cc
main.o: main.cc
$(CC) $(CFLAGS) main.cc -L. -lmylib
error i am getting:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lmylib
collect2.exe: error: ld returned 1 exit status
makefile:28: recipe for target 'main.o' failed
mingw32-make: *** [main.o] Error 1
any help and correction to make this work is really appreciated!
So a few things wrong. When you are using $(CC)
for compilation only, you must specify -c
. So
b1.o: b1.cc b1.h
$(CC) -c $(CFLAGS) -fPIC b1.cc
a1.o: a1.cc
$(CC) -c $(CFLAGS) -fPIC a1.cc
Going from main.cc to main.o is a compilation step, so doesn't need the shared library (but does need the -c
option). So
main.o: main.cc
$(CC) -c $(CFLAGS) main.cc
The program linking step does need the library options and should mention the file being produced (rather than all
). So
runProg: main.o mylib.so
$(CC) main.o -o runProg -L. -lmylib
Haven't checked anything, but hopefully that helps. No doubt if anything is wrong it will be corrected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.