简体   繁体   English

在 makefile 中找不到.so 库 -l

[英]can't find .so library -l in makefile

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.我是makefile的新手,我想制作一个.so库并在main.o文件中使用它,我想使用当前目录中的库。 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 .当您仅使用$(CC)进行编译时,您必须指定-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).从 main.cc 到 main.o 是一个编译步骤,因此不需要共享库(但确实需要-c选项)。 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 ).程序链接步骤确实需要库选项,并且应该提及正在生成的文件(而不是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.毫无疑问,如果有任何错误,它将被纠正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM