0

So I'm making a project where I need a library called dpp. Here is the library structure:

enter image description here

Here is my CMakeLists.txt file:


# Setup vcpkg.
if(NOT DEFINED ENV{VCPKG_ROOT})
    set(ENV{VCPKG_ROOT} cmake/vcpkg)
endif()

if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
            CACHE STRING "")
endif()

project(app)

# Project settings.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Link dpp library.
set(dpp_DIR "${CMAKE_SOURCE_DIR}/libs/dpp")

# Link libraries.
include_directories("${dpp_DIR}/include/dpp-9.0")
link_directories("${dpp_DIR}/lib/dpp-9.0")

# Assign the include directories.
include_directories("${CMAKE_SOURCE_DIR}/include")

# Gather the header and source files.
file(GLOB_RECURSE INC_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE SRC_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp")

# Build.
add_executable(app ${INC_FILES} ${SRC_FILES})
set_target_properties(app PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Link libraries.
target_link_libraries(app dpp)

# Unit tests.
enable_testing()
add_subdirectory(tests)

Now when I build my project I get undefined reference errors

[ 20%] Linking CXX executable bin\app.exe
c:/progra~1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\app.dir/objects.a(hooks.cpp.obj):hooks.cpp:(.text+0x177): undefined reference to `__imp__ZN3dpp5embedC1Ev'
c:/progra~1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\app.dir/objects.a(hooks.cpp.obj):hooks.cpp:(.text+0x1fb): undefined reference to `__imp__ZN3dpp5embedD1Ev'
c:/progra~1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\app.dir/objects.a(hooks.cpp.obj):hooks.cpp:(.text.unlikely+0x5): undefined reference to `__imp__ZN3dpp5embedD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make[3]: *** [CMakeFiles\app.dir\build.make:143: bin/app.exe] Error 1
mingw32-make[2]: *** [CMakeFiles\Makefile2:99: CMakeFiles/app.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:106: CMakeFiles/app.dir/rule] Error 2
mingw32-make: *** [Makefile:133: app] Error 2

I've been stuck in this for hours, I would appreciate any help thanks!

Rami Menai
  • 50
  • 7
  • You probably want to look at dpp.cmake to see what targets it exports. Also for dpp you likely should use find_package(dpp REQUIRED) – drescherjm Nov 21 '21 at 16:58
  • 1
    is DPP built with the same compiler as your project? I'd guess you are using a visual studio library which wont work with gcc – Alan Birtles Nov 21 '21 at 17:15
  • @AlanBirtles That was it! I thought that compiled libraries will work in any compiler. – Rami Menai Nov 21 '21 at 17:48
  • With `c++` code there will be incompatibilities. msvc is binary compatible with VS2015 to VS2022 but not binary compatible with mingw or any prior version of msvc. – drescherjm Nov 21 '21 at 18:47
  • With vcpkg there are mingw triplets and some documentation here: [https://github.com/microsoft/vcpkg/blob/master/docs/users/mingw.md](https://github.com/microsoft/vcpkg/blob/master/docs/users/mingw.md) – drescherjm Nov 21 '21 at 18:48

0 Answers0