0

I am new using cmake and qt. I was working on migrating a project I developed on visual studio and decided to use cmake. I had used .lib libraries in that project but I am having trouble adding the libraries in qt. My CMakeLists.txt file runs but when I build the project from qt it give me "undefined reference to ..."

Here is my CMakeLists.txt file

cmake_minimum_required(VERSION 3.14)

project(LibTest LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(LIB_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/)
set(LIB_TEST ${CMAKE_CURRENT_SOURCE_DIR}/lib/test.lib)

#find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
#find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)


add_executable(LibTest  main.cpp lib/test.lib)
target_include_directories(LibTest PUBLIC ${LIB_TEST_DIR})

add_library(test.lib STATIC IMPORTED ${LIB_TEST})
set_property(TARGET test.lib PROPERTY IMPORT_LOCATION ${LIB_TEST_DIR})

#target_link_libraries(LibTest Qt${QT_VERSION_MAJOR}::Core)

target_link_libraries(LibTest INTERFACE test.lib)

install(TARGETS LibTest
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

main.cpp

#include "libtest.hpp"




int main(int argc, char *argv[])
{
    libtest test;
    //QCoreApplication a(argc, argv);
    return 0;
}

Error When I build the project:

:-1: error: /LibTest/main.cpp:9: undefined reference to `libtest::libtest()'

  • `target_link_libraries(LibTest INTERFACE test.lib)` - Keyword INTERFACE means to not link `LibTest` itself but link only its users. Instead use PUBLIC or PRIVATE keyword (their effect is the same for the executable target). – Tsyvarev Sep 13 '22 at 21:19
  • @Tsyvarev I have tried it with PUBLIC and the issue still persists. It seems to be an issue with QT if you have any more suggestion for troubleshooting. Thanks – DukeOfEast Sep 14 '22 at 15:25
  • The "undefined reference" error means that given function is defined in none files passed to the linker. So the issue either with your CMake code, which doesn't link with the `test.lib` library, or with the library itself, which doesn't define given symbol. QT is absolutely unrelated to that problem. If you want us to help you, then you need to update the question post to contain `CMakeLists.txt` which **actually links** with library (the current code does not) and to contain the library itself (e.g. its C++ code and the way how do you build it). – Tsyvarev Sep 14 '22 at 20:33

0 Answers0