C++ 链接到共享库时,cmake 中出现“No rule to make target”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26807329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"No rule to make target" error in cmake when linking to shared library
提问by Karnivaurus
In Ubuntu, I have downloaded a third-party shared library, mylibrary.so
, which I have placed in the directory /home/karnivaurus/Libraries
. I have also placed the associated header file, myheader.h
, in the directory /home/karnivaurus/Headers
. I now want to link to this library in my C++ code, using cmake. Here is my CMakeLists.txt file:
在 Ubuntu 中,我下载了一个第三方共享库mylibrary.so
,并将其放在目录/home/karnivaurus/Libraries
. 我还放置了相关的头文件,myheader.h
在目录中/home/karnivaurus/Headers
。我现在想在我的 C++ 代码中使用 cmake 链接到这个库。这是我的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.0.0)
project(DemoProject)
include_directories(/home/karnivaurus/Headers)
add_executable(demo demo.cpp)
target_link_libraries(demo /home/karnivaurus/Libraries/mylibrary)
However, this gives me the error message:
但是,这给了我错误消息:
:-1: error: No rule to make target `/home/karnivaurus/Libraries/mylibrary', needed by `demo'. Stop.
What's going on?
这是怎么回事?
回答by zaufi
You may use a full path to the static library. To link w/ dynamic one, better to use link_directories()
like this:
您可以使用静态库的完整路径。要链接动态的,最好这样使用link_directories()
:
cmake_minimum_required(VERSION 2.0.0)
project(DemoProject)
include_directories(/home/karnivaurus/Headers)
link_directories(/home/karnivaurus/Libraries)
add_executable(demo demo.cpp)
target_link_libraries(demo mylibrary)
and make sure mylibrary
has prefix lib
and suffix .so
in file name (i.e. full name is /home/karnivaurus/Libraries/libmylibrary.so
).
并确保文件名中mylibrary
有前缀lib
和后缀.so
(即全名是/home/karnivaurus/Libraries/libmylibrary.so
)。
To make you project more flexible, you'd better to write a finder moduleand avoid hardcode paths like /home/karnivaurus/*
为了让你的项目更灵活,你最好编写一个finder 模块并避免像这样的硬编码路径/home/karnivaurus/*