“No rule to make target” error in cmake when linking to shared library

倖福魔咒の 提交于 2019-12-04 03:17:28

问题


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:

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?


回答1:


You may use a full path to the static library. To link w/ dynamic one, better to use link_directories() like this:

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).

To make you project more flexible, you'd better to write a finder module and avoid hardcode paths like /home/karnivaurus/*



来源:https://stackoverflow.com/questions/26807329/no-rule-to-make-target-error-in-cmake-when-linking-to-shared-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!