How to link any DLL to Cmake project [duplicate] - c++

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
)

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.

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

CMake Linking Instructions [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 3 years ago.
I am trying to link a shared object on my executable with an executable in my cmake project.
my CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (proto_app)
add_executable( helloDemo hello.cpp )
add_executable( faissDemo 1-Flat.cpp )
My executable faissDemo doesn't compile like this because the shared object is not linked. The shared object is located in /usr/local/lib/libfaiss.so. My question is how should I link to the shared object in CMake?
I can compile 1-Flat.cpp successfully from the command line like:
$ g++ 1-Flat.cpp -L /usr/local/lib/ -lfaiss
You need to add the dir to the link_directories and add the lib as target_link_libraries:
cmake_minimum_required (VERSION 2.8)
project (proto_app)
link_directories(/usr/local/lib)
add_executable( helloDemo hello.cpp )
add_executable( faissDemo 1-Flat.cpp )
target_link_libraries(faissDemo faiss)

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

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)

GTest build first at CMake [duplicate]

This question already has answers here:
How to start working with GTest and CMake
(11 answers)
Closed 6 years ago.
I'm building dynamic library and want to use gtest for testing.
SET(GTEST_LIBRARY libs/googletest-master)
set(GTEST_INCLUDE_DIR libs/googletest-master/googletest/include)
#set(GTEST_MAIN_LIBRARY libs/googletest-master/googletest/include/gtest)
#find_package(PostgreSQL REQUIRED)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
But, berofe setting GTEST_MAIN_LIBRARY I have to build it first.
How I can configure CMake to achieve this
Build gtest with Cmake && make (on unix)
Get appropriate path to GTEST_MAIN_LIBRARY
continue build
I copied gtests srcs into project
make a build with Make
and add this to CMake
add_subdirectory(libs/googletest-master)
SET(GTEST_LIBRARY libs/googletest-master)
set(GTEST_INCLUDE_DIR libs/googletest-master/googletest/include)
set(GTEST_MAIN_LIBRARY libs/googletest-master/googlemock/gtest)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
If your put the source for googletest in a subdirectory parented where your CMakeLists.txt file is, the following should work:
add_subdirectory(./googletest)
add_executable(your_program ${MY_SRC})
add_dependencies(your_program gmock)
add_dependencies(your_program gtest)