I use c++ project with opencv and dlib libraries. I have CMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0)
project(project1)
find_package(OpenCV REQUIRED)
include(FetchContent)
FetchContent_Declare(dlib
GIT_REPOSITORY https://github.com/davisking/dlib.git
GIT_TAG v19.18
)
FetchContent_MakeAvailable(dlib)
add_executable(project1 main.cpp)
target_link_libraries(project1 ${OpenCV_LIBS} dlib::dlib)
target_compile_options(project1 PUBLIC "$<$<CONFIG:RELEASE>:${MY_RELEASE_OPTIONS}>")
I write in cygwin cmake .. -G "Unix Makefiles". I use "Unix Makefiles" to create makefile that use to command "make". After this I write make, but as a result I get error:
collect2: error: ld execution ended with return code 1
make[2]: *** [CMakeFiles/project1.dir/build.make:123: project1.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:115: CMakeFiles/project1.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
What I need to do to fix this problem?
Related
I am currently trying to install the SDL library with C++ and Clion. I watched many youtube videos and googled the errors but still cannot find a way to fix this error.
"C:\Program Files\JetBrains\CLion 2021.1.2\bin\cmake\win\bin\cmake.exe" --build D:\Programing\C++\School\HackClub\Day2\cmake-build-debug --target Day2 -- -j 6
[ 50%] Linking CXX executable Day2.exe
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\Day2.dir\build.make:95: Day2.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:82: CMakeFiles/Day2.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:89: CMakeFiles/Day2.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:123: Day2] Error
Here is my CMAKE file.
project(Day2)
set(CMAKE_CXX_STANDARD 14)
set(SDL2_INCLUDE_DIR D:\\Programing\\C++\\libs\\SDL\\include)
set(SDL2_LIBRARY D:\\Programing\\C++\\libs\\SDL\\lib\\x86)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
target_link_libraries(${PROJECT_NAME} SDL2main SDL2)
I've been trying to get SDL2 to link with my project but I think my cmake file is no good. I get this error
====================[ Build | Chip_8_Interpreter | Debug ]======================
"C:\Program Files\JetBrains\CLion 2018.3.4\bin\cmake\win\bin\cmake.exe" --build "D:\Dropbox\Programming\Chip-8 Interpreter\cmake-build-debug" --target Chip_8_Interpreter -- -j 4
[ 50%] Linking CXX executable Chip_8_Interpreter.exe
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Chip_8_Interpreter.exe] Error 1
CMakeFiles\Chip_8_Interpreter.dir\build.make:87: recipe for target 'Chip_8_Interpreter.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/Chip_8_Interpreter.dir/all] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/Chip_8_Interpreter.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/Chip_8_Interpreter.dir/rule] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/Chip_8_Interpreter.dir/rule' failed
mingw32-make.exe: *** [Chip_8_Interpreter] Error 2
Makefile:117: recipe for target 'Chip_8_Interpreter' failed
And currently my CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.14)
project(Chip_8_Interpreter)
set(CMAKE_CXX_STANDARD 14)
# include cmake/FindSDL2.cmake
set(SDL2_PATH "C:\\SDL2-2.0.12\\i686-w64-mingw32")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(Chip_8_Interpreter main.cpp)
target_link_libraries(Chip_8_Interpreter ${SDL2_LIBRARY} -lmingw32 -lSDL2main -lSDL2 -mwindows)
And my main.cpp is just the basic SDL example. I've also recently installed MinGW and SDL2, so they should both be the newest stable release.
I want to build an executable that includes all of libs that I used (opencv, dlib) so it can work on any computer.
Here is my current CMakeList.txt
cmake_minimum_required(VERSION 3.5.1)
project(adc_fr)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(/media/sf_dlib-19.9/dlib dlib_build)
FIND_PACKAGE(OpenCV REQUIRED)
set(SOURCE_FILES
FDetect.cpp
FDetect.h
FExtract.cpp
FExtract.h
main.cpp
)
add_executable(adc_fr ${SOURCE_FILES})
set_target_properties(adc_fr PROPERTIES LINK_FLAGS "-static")
target_link_libraries(adc_fr
${OpenCV_LIBS}
dlib
)
Here is the error I got when linking
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libwebp.so'
collect2: error: ld returned 1 exit status
CMakeFiles/adc_fr.dir/build.make:409: recipe for target 'adc_fr' failed
make[2]: *** [adc_fr] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/adc_fr.dir/all' failed
make[1]: *** [CMakeFiles/adc_fr.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I do have libwebp.a available in /usr/lib/x86_64-linux-gnu/
How can I force to use static libwebp.a instead of libwebp.so?
I did try to add #SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") but it still gave the same error.
Much appreciated!
How about this:
find_library(LIBWEBP_LOCATION libwebp.a)
target_link_libraries(adc_fr
${OpenCV_LIBS}
dlib
${LIBWEBP_LOCATION}
)
I'm trying to link a downloaded library called ArduinoJson to my project with CMake, but it seems that whenever i include the file it is missing, it will just ask for the next file its missing. I will end up with a lot of include lines which i think isnt the right way. I cant seem to find the correct syntax to include a libary in CMake
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_FLAGS "-DTESTING")
include_directories(
/usr/share/arduino/libraries/ArduinoJson/include
/usr/share/arduino/hardware/arduino/cores/arduino
)
set(files
DataHandler.h
DataHandler.cpp
JSON.cpp
JSON.h
HAL_mock.cpp
HAL_mock.h
HAL_Protocol.h
lib/ArduinoJson/ArduinoJson.h
)
# Locate GTest
find_package(GTest REQUIRED)
# Link runTests with what we want to test and the GTest library
add_executable(runTests DataHandlerTests.cpp JSONTests.cpp HALTests.cpp ${files})
target_link_libraries(runTests GTest::Main ArduinoJson)
The error I get
[ 14%] Building CXX object CMakeFiles/runTests.dir/JSONTests.cpp.o
In file included from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/../String.hpp:14:0,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/DynamicStringBuilder.hpp:11,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:12,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonVariant.hpp:13,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonBuffer.hpp:14,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/BlockJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/DynamicJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.h:8,
from /usr/share/arduino/libraries/ArduinoJson/ArduinoJson.h:8,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSON.h:6,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSONTests.cpp:5:
/usr/share/arduino/hardware/arduino/cores/arduino/WString.h:29:26: fatal error: avr/pgmspace.h: No such file or directory
compilation terminated.
CMakeFiles/runTests.dir/build.make:86: recipe for target 'CMakeFiles/runTests.dir/JSONTests.cpp.o' failed
make[2]: *** [CMakeFiles/runTests.dir/JSONTests.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I would like to add 2 maybe 3 lines which include all the libraries i need for my ArduinoJson library
I'm trying to compile a c++ program that uses xlib with cmake. However, I'm having a problem including and linking xlib libraries in cmake file.
This is the error that I'm getting.
main.cpp:378: undefined reference to `XClearWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/project1.dir/build.make:94: recipe for target 'project1' failed
make[2]: *** [project1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/project1.dir/all' failed
make[1]: *** [CMakeFiles/project1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And when I use just the command line to compile, it works just fine.
I use this command (g++ main.cpp -L/usr/X11R6/lib -lX11)
and this is my cmake file.
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
link_directories(/usr/X11R6/lib)
include_directories(/usr/share/X11)
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
In your case, you forgot to specify the libraries that cmake should use to link your application (target_link_libraries or link_libraries).
But, why not just let cmake find the required path, libraries and includes by itself? I suggest you to use find_package(X11). In your case, you can try:
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_STANDARD 11) # for c++11
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
I won't be able to explain why (if you have some idea, don't hesitate to comment my answer), but, in my case, I had to link X11 library using this command:
target_link_libraries( DisplayImage "-lX11" )
It works, at least, for the 3.5.1 version of cmake.