简体   繁体   English

使用库时如何避免C++编译中出现多个-l

[英]How to avoid multiple -l in C++ compiling when using libraries

I really have met lots of problems while using external libraries in c++.在 c++ 中使用外部库时,我确实遇到了很多问题。 The library includes a include file of header files, and a lib file which includes all.la, .so files.该库包括一个包含 header 文件的包含文件,以及一个包含 all.la、.so 文件的 lib 文件。 I added the library in to usr/local/include and usr/local/lib, and also edited the ld.so.conf file.我将库添加到 usr/local/include 和 usr/local/lib 中,还编辑了 ld.so.conf 文件。 After all these is done, I supposed that my program could be compiled successfully.完成所有这些后,我认为我的程序可以编译成功。 However, an error occured:但是,出现了错误:

$ g++ -o b try.cpp
 /usr/bin/ld: /tmp/ccTWcUvt.o: in function `main':
try.cpp:(.text+0x36): undefined reference to `OsiClpSolverInterface::OsiClpSolverInterface()'
/usr/bin/ld: try.cpp:(.text+0x45): undefined reference to `OsiClpSolverInterface::~OsiClpSolverInterface()'

then I typed all the library file names behind -l manually and compiled again with this command:然后我手动输入了 -l 后面的所有库文件名,并使用以下命令再次编译:

g++ -o b try.cpp -lCbc -lCbcSolver -lCgl -lClp -lcoinasl -lcoinglpk -lcoinmumps -lCoinUtils -lOsi -lOsiCbc -lOsiClp -lOsiCommonTest -lOsiGlpk

This time it did compile successfully and the program worked great.这次它确实编译成功并且程序运行良好。 I'm wondering whether there is any method to avoid this verbosity?我想知道是否有任何方法可以避免这种冗长? Also, if anyone could expain to me why just adding the path to the lib files are not enough, but must point out the names explicitly, I would be so very very grateful as well.另外,如果有人可以向我解释为什么仅添加 lib 文件的路径是不够的,但必须明确指出名称,我也会非常感激。

Thanks a lot!非常感谢!

The linker knows which functions you are calling. linker 知道您正在调用哪些函数。 It does not know which libraries those functions are contained in, and it's not going to go searching through many hundreds or thousands of libraries to find them.它不知道这些函数包含在哪些库中,也不会通过 go 搜索数百或数千个库来找到它们。

With gcc, more so than with Visual C++, the order of the libraries can be important, so they don't even support the pragma to specify libraries.使用 gcc,比使用 Visual C++ 更重要的是,库的顺序可能很重要,因此它们甚至不支持指定库的编译指示。

This is not verbosity.这不是冗长。 You need to be using Makefiles if it is too much typing for you.如果对您来说输入太多,您需要使用 Makefiles。

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

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