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.