CMake的static_directory问题与静态库(在Mac OS X上) [英] CMake's link_directories issue with static library (on Mac OS X)
问题描述
我在/ PATH目录中有一个静态库,当我试图使用 link_directories
的库如下:
I have a static library in /PATH directory, and when I tried to use the library with link_directories
as follows:
link_directories(/PATH)
target_link_libraries(CppHello libHelloLib.a)
我有一个错误消息:
ld: library not found for -lHelloLib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/PATH] Error 1
make[2]: *** [CMakeFiles/CppHello.dir/all] Error 2
make[1]: *** [CMakeFiles/CppHello.dir/rule] Error 2
相反,我必须指定如下路径才能使其工作:
Instead, I had to specify the path as follows to make it work:
target_link_libraries(CppHello /PATH/libHelloLib.a)
可能有什么问题?这是Cmake在Mac OS X上的问题,或者我只是错过了什么?
What might be wrong? Is this an issue with Cmake on Mac OS X, or did I just miss something?
推荐答案
在 link_directories() >创建可执行文件: link_directories
仅影响在其后创建的目标: https://cmake.org/cmake/help/v3.4/command/link_directories.html 。结果是,将正确的 -lHelloLib
标志添加到目标,但是lib搜索路径未更新为 -L / PATH
。
You call link_directories()
after creating executable: link_directories
affects only on targets, created after it: https://cmake.org/cmake/help/v3.4/command/link_directories.html. The result is that the correct -lHelloLib
flag is added to the target, but the lib search path isn't updated with a -L/PATH
flag.
在创建任何目标之前,请调用 link_directories()
Instead put the call to link_directories()
before you create any targets.
自从3.3版本CMake文档 target_link_libraries 明确指定了它接受的链接的哪种类型的项目。其中:
Since 3.3 version CMake documentation for target_link_libraries explicitely specifies, which kind of items for link it accepts. Among them:
- 库文件的完整路径
- >
- A full path to a library file
- A plain library name
因此,您应该指定库文件的完整路径或 / strong>,没有文件扩展名( .a
)和前缀( lib
)。在这种情况下,错误信息显示,CMake试图处理甚至只有文件名库,但没有成功(某种类型的未定义行为)。
So, you should specify either full path to the library file, or only name for the library, without file's extension(.a
) and prefix(lib
). Error message in you case shows, that CMake has tried to handle even filename-only library, but without success(some sort of Undefined Behaviour).
虽然以前的CMake版本没有记录这个命令清楚,他们可能遵循相同的约定。
While previous versions of CMake doesn't document this command so clear, they, probably, follow same convention.
这篇关于CMake的static_directory问题与静态库(在Mac OS X上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!