0

I am investigating using CLion and CMake. I have set up a Cygwin and created a new project which is working fine in terms of setting up the build environment, and now I've tried creating a new library which successfully builds so now I am trying to link it to my main project however it keeps failing.

The library project is called TestLib if that makes a difference.

Below is my CMakeList.txt file of my library file.

cmake_minimum_required(VERSION 3.17)
project(TestLib CXX)

set(CMAKE_CXX_STANDARD 14)

add_library(TestLib STATIC includes/Test.h Test.cpp)

Below is the CMakeList.txt file for my main project

cmake_minimum_required(VERSION 3.17)
project(HelloWorld CXX)
set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_STANDARD 14)

include_directories(include ../TestLib/includes)

add_library(TestLib STATIC "../TestLib/cmake-build-debug" "../TestLib/includes/")

SET_TARGET_PROPERTIES(TestLib PROPERTIES LINKER_LANGUAGE CXX)
LINK_DIRECTORIES("D:/CLion Projects/TestLib/cmake-build-debug/")

add_executable(HelloWorld main.cpp)

TARGET_LINK_LIBRARIES(HelloWorld TestLib)

Below is the output from the make (I've only included the bit at the bottom after the build completes otherwise there's quite a lot to put here

[100%]Linking CXX executable HelloWorld.exe
/cygdrive/c/Users/Chris/AppData/Local/JetBrains/CLion2020.2/cygwin_cmake/bin/cmake.exe -E cmake_link_script CMakeFiles/HelloWorld.dir/link.txt --verbose=1
/usr/bin/c++.exe -g  -Wl,--enable-auto-import CMakeFiles/HelloWorld.dir/main.cpp.o  -o HelloWorld.exe -Wl,--out-implib,libHelloWorld.dll.a -Wl,--major-image-version,0,--minor-image-version,0   -L"/cygdrive/d/CLion Projects/HelloWorld/D:/CLion Projects/TestLib/cmake-build-debug"  libTestLib.a 
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/HelloWorld.dir/main.cpp.o:/cygdrive/d/CLion Projects/HelloWorld/main.cpp:18: undefined reference to `Test::sayHelloFromLib(std::string&, std::string&)'
CMakeFiles/HelloWorld.dir/main.cpp.o: in function `main':
/cygdrive/d/CLion Projects/HelloWorld/main.cpp:18:(.text+0xf4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Test::sayHelloFromLib(std::string&, std::string&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/HelloWorld.dir/build.make:108: HelloWorld.exe] Error 1
make[2]: Leaving directory '/cygdrive/d/CLion Projects/HelloWorld/cmake-build-debug'
make[1]: *** [CMakeFiles/Makefile2:128: CMakeFiles/HelloWorld.dir/all] Error 2
make[1]: Leaving directory '/cygdrive/d/CLion Projects/HelloWorld/cmake-build-debug'
make: *** [Makefile:107: all] Error 2

The method Test::sayHelloFromLib(std::string&, std::string&) which is saying is an undefined symbol is from my library.

Inside ../TestLib/cmake-build-debug contains the following files:

  • cmake_install.cmake
  • CMakeCache.txt
  • cygTestLib.dll
  • libTestLib.a
  • libTestLib.dll.a
  • Makefile
  • TestLib.cbp

In the add_library method I've given it the path of the cmake-build-debug which contains the files above so I would have expected it would find the library file above. I've also tried changing this path to point to the libTestLib.a and libTestLib.dll.a but I get the same problem.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 1
    Does this answer your question? [CMake link to external library](https://stackoverflow.com/a/10550334/3987854) The `add_library` command accepts **source files** as its arguments, not directories or library files. If you want to add an external library to your CMake project, see the linked answer for the best way (use of `link_directories()` is *discouraged*). Alternatively, you could just add the new `TestLib` CMake file to your existing CMake project (using `add_subdirectory`) to avoid referencing the external library's path altogether. – Kevin Sep 11 '20 at 15:04
  • The linked question is not related to the problem I was having – Boardy Sep 11 '20 at 16:21
  • 1
    The linked question asks how to link an *existing* external library to a CMake project. Your `TestLib` library (as your question describes) appears to exist (i.e. already built) when you are configuring your main CMake project. However, your CMake code doesn't properly import/link this external library, later yielding linker errors. This makes the linked question (specifically the linked answer) directly applicable to your problem. If you think we have misunderstood your question, could you add some details to your question post to clarify the issue? – Kevin Sep 11 '20 at 17:55

0 Answers0