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

40,068

While the other answer posted here is valid, it is out-dated. CMake now provides better solutions for using a pre-built external library in your code. In fact, CMake itself even discourages the use of link_directories() in its documentation.

The target_link_libraries() command takes very specific syntax for linking to an external library. A more modern solution is to create an IMPORTED CMake target for your external library:

add_library(MyExternalLib SHARED IMPORTED)

# Provide the full path to the library, so CMake knows where to find it.
set_target_properties(MyExternalLib PROPERTIES IMPORTED_LOCATION /home/karnivaurus/Libraries/mylibrary.so)

You can then use this imported CMake target later on in your code, and link it to your other targets:

target_link_libraries(demo PRIVATE MyExternalLib)

For other ways to use an external third-party library in your CMake code, see the responses here.

Share:
40,068
Karnivaurus
Author by

Karnivaurus

Updated on July 09, 2022

Comments

  • Karnivaurus
    Karnivaurus almost 2 years

    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?

  • steveire
    steveire over 9 years
    Use find_file and friends instead.
  • zaufi
    zaufi over 9 years
    @steveire often just having some path to a file is not enough. for more serious projects it is better to learn finders and how to write them.
  • steveire
    steveire over 9 years
    That's not your suggestion.
  • tjwrona1992
    tjwrona1992 almost 5 years
    This answer is very outdated. The current CMake documentation explicitly states that link_directories should be avoided. Refer to the documentation for more information on alternatives.
  • tjwrona1992
    tjwrona1992 almost 5 years
    I don't consider it a waste of time and I'm not trying to insult your answer. I'm not saying it was a bad answer when it was written, but people will stumble across this thread on Google looking for CMake advice. They should be made aware that although it may have been a good answer in the past it would be bad to take this advice today because there are better alternatives.
  • Kevin
    Kevin almost 4 years
    @astronomerdave The IMPORTED_LOCATION property should point to a single target. I believe you can set two versions of a library for one target, using IMPORTED_LOCATION_Debug and IMPORTED_LOCATION_Release, for example.