How to link shared library *dll with CMake in Windows [duplicate] - c++

This question already has answers here:
CMake link to external library
(6 answers)
Closed 12 months ago.
I have 2 files: library.dll and library.h with some code that I need in my own project. I'm working on Windows with Clion where I should config this with CMake.
I tried this way:
cmake_minimum_required(VERSION 3.6)
project(test2)
set(CMAKE_CXX_STANDARD 11)
link_directories(C:\\Users\\Johny\\CLionProjects\\test2)
set(SOURCE_FILES main.cpp)
add_executable(test2 ${SOURCE_FILES})
target_link_libraries(test2 library.dll)
It compiled but didnt work. Returns code -1073741515
How can I handle with it?

Although this question is old. You are targeting the link library wrongly.
target_link_libraries(test2 library.dll) is wrong. This is an example linking SDL2. In the main CMakeList.txt
cmake_minimum_required(VERSION 3.12)
project(GraphicTest)
set(CMAKE_CXX_STANDARD 11)
include_directories("${PROJECT_SOURCE_DIR}/SDL")
add_subdirectory(SDL)
add_executable(GraphicTest main.cpp)
target_link_libraries(GraphicTest SDL2)
and in the library folder. Here SDL, add a CMakeLists.txt
message("-- Linking SDL")
add_library(SDL2 SDL2.dll)
set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE C)

Related

How to link multiple libraries reside in different subfolders in CMake and using namespace? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I have started to learn CMake framework to build cmake recently and started a dummy project to understand how it works.
My project structure is as shown in the image. The CMakeLists.txt at the root level (myapp->CMakeLists.txt) is shown below:
cmake_minimum_required(VERSION 3.20)
project(dummyCMake)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "-o0")
add_subdirectory(libs)
include_directories(includes)
file(GLOB SOURCES "src/*.cpp")
add_executable(${PROJECT_NAME} ${SOURCES})
get_filename_component(parent_dir ../ ABSOLUTE)
target_link_libraries(${PROJECT_NAME} PUBLIC uiolibs)
target_link_libraries(${PROJECT_NAME} PUBLIC mathlibs)
target_include_directories(${PROJECT_NAME} PUBLIC
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/libs/uiolibs/includes"
"${PROJECT_SOURCE_DIR}/libs/mathlibs/includes"
)
sub directories CMakeLists.txt is as follows:
file(GLOB sources "src/*cpp")
message(${sources}) #for debugging
add_library(uiolibs STATIC ${sources})
Getting linking error
undefined reference to `mathlibs::AddInit(int, int)
undefined reference to `userinput::getInit()
Any suggestion to fix this issue (or in general on cmake framework) will be appreciated.
The project code folder: github link
It's good that you linked your code here because it's not a cmake problem.
using namespace userinput;
int getInit() {}
This doesn't define userinput::getInit as you expect, it just defines getInit.

how to link .so files with CMake [duplicate]

This question already has answers here:
cmake and libpthread
(4 answers)
Undefined reference to pthread_create in Linux
(16 answers)
Closed 2 years ago.
I want to rebuild a simple application based on a .cpp, a .h and multiple .so files. From what i've seen, my CMakeLists.txt should be like this :
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project(test C CXX)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(test ${CMAKE_SOURCE_DIR}/libA.so ${CMAKE_SOURCE_DIR}/libB.so)
All files are in the same folder. I previously linked my .cpp with my .h file correctly. cmake . is giving me no error, but after using make i get :
main.cpp:(.text+0xf2d) : undefined reference to « pthread_create »
Which is a function that doesn't belong to my .h file so it should be in the .so file. I don't know if the issue comes from the link or the file .so itself.
I also have file with the same name like libA.so, libA.so.0 or libA.so.0.2, should i include all of these files in my executable ?
The error message means that you have to add pthread to the list of linked libraries. In target_link_libraries you only list the library names without path, lib prefix and file extension:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project(test C CXX)
find_package(Threads REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(test A B Threads::Threads)
You can add paths with target_link_directories:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project(test C CXX)
find_package(ThreadsREQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_directories(test PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(test PRIVATE A B Threads::Threads)
The good way to do it is to define respective target which will represent library.
add_library(externalLibA SHARED IMPORTED)
set_target_properties(externalLibA
PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libA.so)
target_include_directories(externalLibA
INTERFACE ${CMAKE_SOURCE_DIR}/lib)
Then add this target as dependency of your target.
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC externalLibA)

CMAKE GLFW linking problems [duplicate]

This question already has answers here:
Getting a CMake Error: Cannot specify link libraries for target which is not built by the project
(4 answers)
Closed 2 years ago.
I wanto add glfw dependency by cmake in CLion but i get weird error, this is the CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(hello_gl)
set(CMAKE_CXX_STANDARD 14)
find_package(glfw3 3.3 REQUIRED)
target_link_libraries(hello_gl glfw)
add_executable(hello_gl main.cpp)
CMake Error at CMakeLists.txt:8 (target_link_libraries):
Cannot specify link libraries for target "hello_gl" which is not built by
this project.
This is the error I get? How it comes that cmake cannot find my app?
It worked when I tried to link glfw lib after adding the executable
cmake_minimum_required(VERSION 3.17)
project(hello_gl)
set(CMAKE_CXX_STANDARD 14)
add_executable(hello_gl main.cpp)
find_package(glfw3 3.3 REQUIRED)
target_link_libraries(hello_gl glfw)
Here is the right CMakeLists.txt

Link restbed with Cmake [duplicate]

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
Closed 2 years ago.
In trying to link restbed with CMake I get the usual undefined function error. However, trying the exact same code linked with g++ test.cpp -o test -lrestbed works fine.
Furthermore, when I first implemented the CMakeLists.txt it also worked fine and as I added to the project it started facing issues. Now even a single restbed function is not defined.
My restbed includes are located at /usr/local/include and the shared objects to link at /usr/local/lib. Pretty standard locations.
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(vcar-server)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -lrestbed")
file(GLOB CXX_EXEC "src/*.cpp")
add_subdirectory(vcar-embedded)
add_executable(vcar-server ${CXX_EXEC})
target_link_libraries(vcar-server vcar)
Do not use CMAKE_CXX_FLAGS, or rather use it as a "last resort" when configuring the build system as a user. Prefer to use object model in place of global variables. The modern cmake way would most probably be something along:
cmake_minimum_required(VERSION 3.11)
project(vcar-server)
add_subdirectory(vcar-embedded)
find_package(Threads REQUIRED)
add_executable(vcar-server ${cxx_exec})
target_link_libraries(vcar-server PUBLIC vcar restbed Threads::Threads)
set_target_properties(vcar-server PUBLIC CXX_STANDARD 20)

How to link any DLL to Cmake project [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 3 years ago.
Is job in qmake:
LIBS+= -L"C:\Program Files\program\any_dll.dll"
How to in cmake?
cmake_minimum_required(VERSION 2.8)
project(my_project)
add_executable(${PROJECT_NAME} "main.cpp")
If CMake does not now a target (i.e. it is not build by CMake) it assumes it to be a prebuild library.
link_directories(
"C:/Program Files/program"
)
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME}
any_dll
)