0

I am trying to add an external .lib file to my project in Clion which uses CMake. My code is very simple and is simply to test whether the library gets included:

#include <iostream>
#include "header/test.h"
int main() {
test a; // returns error saying undefined reference to 'test::test()'
return 0;
}

When running this code I get the following error:

 undefined reference to `test::test()'

This is because I am trying to make a test object however the library for test is not included.

The test.lib file and the test.h file are both in the "header" folder which is in the root of my project folder. The file path to this is F:\Project\header\

My Cmake text file is as follows:

cmake_minimum_required(VERSION 3.14)
project(Project)

set(CMAKE_CXX_STANDARD 14)

add_executable(Project main.cpp)
target_link_libraries(Project 
F:\\Project\\header\\test.lib)

In the cmake text file i use the line: target_link_libraries(Project F:\Project\header\test.lib)

This should include the library file, however it doesn't seem to because I get the "undefined reference to..." error as mentioned above. The Cmake compiler does not give me an error.

  • 2
    Usually, finding a library is done with `find_package`. What is the library you're trying to use? Maybe they exports their targets correctly and you only need a prefix path – Guillaume Racicot Aug 21 '19 at 15:54
  • The `CMakeLists.txt` is correct, and linking with the library has been performed. It seems that your `F:\Project\header\test.lib` does **not define** given function (`test::test`) or does that in **incompatible** manner. The first alternative can be checked with inspected the code of `test.lib` **sources** (add that code to the question post). As for the second alternative, how do you build you `test.lib` library? By using CLion too, or with other compiler? – Tsyvarev Aug 21 '19 at 17:42

1 Answers1

1

You are conceptually correct, however you are not doing it in the CMake fashion. Check out the following links on how to link an external library.

CMake link to external library

cmake doesn't support imported libraries?

https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Exporting-and-Importing-Targets

For your case, it would be as follows):

cmake_minimum_required(VERSION 3.14)
project(Project)

set(CMAKE_CXX_STANDARD 14)

# Import the library into the CMake build system
ADD_LIBRARY(test SHARED IMPORTED)

# Specify the location of the library 
SET_TARGET_PROPERTIES(TARGET test PROPERTIES IMPORTED_LOCATION “/path/to/lib/test.dll”)

# create the executable   
add_executable(Project main.cpp)

# Link your exe to the library
target_link_libraries(Project test)

The CMake documentation is very good. I recommend checking it out when you run into issues.

https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries

GLJ
  • 1,074
  • 1
  • 9
  • 17
  • 1
    With an imported library you need to set the `IMPORTED_LOCATION` and the `INTERFACE_INCLUDE_DIRECTORY` – Guillaume Racicot Aug 21 '19 at 15:56
  • I see. I have tried what you said and have moved test.lib into the folder with my cmake text file. I now get the error: `No rule to make target 'test-NOTFOUND', needed by 'Project.exe'. Stop.` – Algoliahelpmeplz Aug 21 '19 at 16:07
  • @GuillaumeRacicot you are correct, thank you. I’ll update my answer. – GLJ Aug 21 '19 at 16:14
  • I have tried what you said plus a variety of more things including moving my .lib file around. My code is the exact same as yours with the exception: `set_target_properties(test PROPERTIES IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/test.lib")` The error returned is `cannot find -llibrary collect2.exe: error: ld returned 1 exit status` Any idea why? My .lib file is in the same directory as my cmakelist.txt and I know that `${CMAKE_SOURCE_DIR}/test.lib` points to it. – Algoliahelpmeplz Aug 21 '19 at 17:30
  • @Algoliahelpmeplz: Looks like you use `library` (literally!) in your `target_link_libraries` call. This is wrong: you need to link with `test`, which is the name of the **library target** corresponded to `test.lib`. – Tsyvarev Aug 21 '19 at 17:47
  • 1
    Given answer doesn't fix the problem stated in the question: After fix all issues with this `CMakeLists.txt`, it calls the compiler and the linker with the **same parameters**, as in the question post. So it would emit the same error as the asker wants to resolve. Then, why given answer is here? If you want to show a **generic way** for link external library, you need to answer [CMake link to external library](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) question, not a given one. – Tsyvarev Aug 21 '19 at 17:55