CMAKE GLFW linking problems [duplicate] - c++

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

Related

Some (but not all) targets in this export set were already defined

I create a CMakeLists.txt and the content is as followed
cmake_minimum_required (VERSION 3.8)
project(CTP_dll)
add_library(CTPdll SHARED CTPdll.cpp)
add_executable(CTPTest CTPTest.cpp)
target_link_libraries(CTPTest CTPdll)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(CTPdll ${OpenCV_LIBS})
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
target_link_libraries(CTPTest ${VTK_LIBRARIES})
And the error info is
CMake Error at D:/vcpkg/installed/x64-windows/share/hdf5/hdf5-targets.cmake:37 (message):
Some (but not all) targets in this export set were already defined.
Targets Defined: hdf5::hdf5-shared;hdf5::hdf5_hl-shared
Targets not yet defined: hdf5::hdf5_cpp-shared;hdf5::hdf5_hl_cpp-shared
If I delete the including of VTK as followed, no error will be reported. But obvious I can't include VTK in CTPTest.cpp, which is unacceptable.
cmake_minimum_required (VERSION 3.8)
project(CTP_dll)
add_library(CTPdll SHARED CTPdll.cpp)
add_executable(CTPTest CTPTest.cpp)
target_link_libraries(CTPTest CTPdll)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(CTPdll ${OpenCV_LIBS})
I compile this with Visual Studio 2022 on windows10 platform.
There was a similar question on the web but nobody replied. So I propose this question and hope someone can help.
Seems you are encountering vcpkg issue #15502.
There is a pull request with a fix available, which apparently was not merged yet.

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.

Installing libzippp library with cmake not working

Trying to install libzippp library in this project is not working for some reason, here is my project's CMakeLists.txt:
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(startProject)
set(Leptonica_DIR /Users/alejandrocamba/Documents/leptonica/build)
find_package(OpenCV REQUIRED)
find_package(Leptonica REQUIRED)
find_package(Tesseract REQUIRED)
find_package(libzippp 3.0 REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${Leptonica_INCLUDE_DIRS})
include_directories(${Tesseract_INCLUDE_DIRS})
add_executable(startProject main.cpp)
target_link_libraries(startProject libzipp::libzipp)
target_link_libraries(startProject ${OpenCV_LIBS})
target_link_libraries(startProject ${Tesseract_LIBRARIES})
I'm getting the error:
ld: library not found for -llibzipp::libzipp
I have followed the instructions and i cloned the repository and successfully installed it, so if i do make install i get:
But i can't seem to find a way to use it on my project, i need help to make get it working on my project!
Need more p: the proper target name for link with is libzippp::libzippp.
The project's README wrongly suggests to use libzipp::libzipp.

How do I add OpenCV to LD_LIBRARY path in linux?

I used this link to install OpenCV.
What works:
1.OpenCV works fine with python (running from terminal).
2.I can import opencv libraries in a single C++ program.
What does not work :
When the code is spread across multiple and you need to build it using CMake.
Here's my CmakeLists.txt :
1.cmake_minimum_required(VERSION 3.9)
2.project(Image_processing)
3.set(CMAKE_CXX_STANDARD 14)
4.find_package(OpenCV REQUIRED)
5.include_directories(/home/user/opencv/build)
6.add_executable(main main.cpp)
7.target_link_libraries(project_name ${OpenCV_LIBS})
Errors (can regenerate them by commenting lines 4,5 and 7 in above CMake file):
undefined reference to OpenCV functions.
CMake Error at CMakeLists.txt:7 (target_link_libraries):
Cannot specify link libraries for target "Image_processing" which is not
built by this project.
Correct it with:
cmake_minimum_required(VERSION 3.5)
project(Image_processing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
In your CMakeLists.txt, the exe-name is not matching with the target-link-name. I modify the line, then it works on my PC.
The CMakeLists.txt of an OpenCV Project:
cmake_minimum_required(VERSION 3.5)
project(Image_processing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
#include_directories(/home/user/opencv/build)
add_executable(Image_processing main.cpp)
target_link_libraries(Image_processing ${OpenCV_LIBS})

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)