CMake and Window .Lib Files - c++

I have been struggling to find an answer to this question across Stackoverflow, at least one that I understand. I recently bought CLion and want to port an older project from Visual Studio over to it. But I have no idea how to link or add .lib files to my project. I need to link Xinput and DSound.
In Visual Studio I just added the headers and then added these to the top of my file
#pragma comment(lib, "XInput.lib")
#pragma comment(lib, "Dsound.lib")
I just have absolutely no idea how to link to those libs using CMake as I am a complete beginner with it.
Any Help would be gladly appreciated
This is my current CMake file
cmake_minimum_required(VERSION 3.3)
project("Handmade_Hero")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
add_executable("Handmade_Hero" ${SOURCE_FILES})
UPDATE
cmake_minimum_required(VERSION 3.3)
project(Handmade_Hero)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
add_executable(Handmade_Hero ${SOURCE_FILES})
set( LIBS XInput DSound )
target_link_libraries(Handmade_Hero ${LIBS} )
My Compiler now throws the following errors:
PATH\ClionProjects\Handmade-Hero\main.cpp:3:20: fatal error: Xinput.h: No such file or directory
#include <Xinput.h>
^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Handmade_Hero.dir/main.cpp.obj] Error 1
CMakeFiles\Handmade_Hero.dir\build.make:62: recipe for target 'CMakeFiles/Handmade_Hero.dir/main.cpp.obj' failed
mingw32-make.exe[2]: *** [CMakeFiles/Handmade_Hero.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Handmade_Hero.dir/rule] Error 2
mingw32-make.exe: *** [Handmade_Hero] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Handmade_Hero.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Handmade_Hero.dir/rule' failed
Makefile:117: recipe for target 'Handmade_Hero' failed

Simply add these after your CMakeLists.txt
SET( LIBS XInput DSound )
TARGET_LINK_LIBRARIES(projectname ${LIBS} )

Related

How to link libraries properly using CMakeLists.txt for MinGW?

I have been pulling out of my hair because of CMakelists.txt. I would like to create a C++ application with GLFW, GLEW libraries, using MinGW and CLion IDE.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.17.3)
project(imguiTask)
set(CMAKE_CXX_STANDARD 17)
add_compile_options(-DGLEW_NO_GLU)
add_executable(${PROJECT_NAME}
src/main.cpp
src/imgui.cpp
src/imgui_draw.cpp
src/imgui_widgets.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC includes)
add_library(libraries STATIC IMPORTED)
target_link_libraries(${PROJECT_NAME} glfw3)
I placed the needed libraries in the libraries folder as you can see in the picture. The folder is located in the project root. It is giving me the following error:
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [imguiTask.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/imguiTask.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/imguiTask.dir/rule] Error 2
mingw32-make.exe: *** [imguiTask] Error 2
It seeems to me, that it does not search in the folder I specified. I don't know why.

C++ ZBar cmake error 'File not found' when including

I have a problem when I include ZBar in my C++ script. I already tried adding it via a CMakelists.txt:
cmake_minimum_required(VERSION 2.8.12)
project( Barcode-cpp )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} ${ZBARCV_SOURCE_DIR} )
set(CMAKE_MODULE_PATH ${ZBARCV_SOURCE_DIRS})
add_compile_options(-std=c++11)
add_library( src
src/VideoVeed.h
src/VideoVeed.cpp
src/Crop.h
src/Crop.cpp
src/Barcodes.h
src/Barcodes.cpp
)
add_executable( program
program/main.cpp
)
target_link_libraries( program src ${OpenCV_LIBS} ${ZBAR_LIBRARIES} zbar )
I'm on mac. I looked and my zbar.h file is located in /usr/local/include/ where it's supposed to be.
I include it like this: #include <zbar.h>
I hope someone is able to help me. Thanks in advance!
EDIT:
Full make error log:
/Users/mathijs/Documents/Barcode-cpp/src/Barcodes.h:7:10: fatal error: 'zbar.h' file not found
#include <zbar.h>
^~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/src.dir/src/VideoVeed.cpp.o] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2
I just checked; the Brew package for ZBar includes a packageconfig file (zbar.pc)
That means you can use modern CMake tooling instead of cargo culting:
cmake_minimum_required(VERSION 3.8)
project( Barcode-cpp )
find_package( OpenCV REQUIRED )
include_directories(${OpenCV_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 11)
add_library( src
src/VideoVeed.h
src/VideoVeed.cpp
src/Crop.h
src/Crop.cpp
src/Barcodes.h
src/Barcodes.cpp
)
add_executable( program
program/main.cpp
)
target_link_libraries(program src ${OpenCV_LIBS})
find_package(PkgConfig REQUIRED)
pkg_check_modules(ZBar REQUIRED IMPORTED_TARGET zbar)
target_link_libraries(program PkgConfig::ZBar)
The pkg_check_modules will read the zbar.pc file and generate an IMPORTED target named PkgConfig::ZBar that will automatically set both include paths and linker paths for program.

opencv Cross Compiling with cmake

I am trying to cross compile a c++ project that uses openCV and a camera API for Raspberry Pi but I have the following linker error after the compilation finishes successfully: make[2]: No rule to make target '/opt/vc/lib/libmmal_core.so', needed by 'agv_car'. Stop. although the .so file is in an directory that is included in the search directories(see cmake file below)
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
INCLUDE_DIRECTORIES($ENV{HOME}/rpi/rootfs/opt/vc/include)
INCLUDE_DIRECTORIES($ENV{HOME}/rpi/rootfs/opt/vc/include/interface/vcos/pthreads)
INCLUDE_DIRECTORIES($ENV{HOME}/rpi/rootfs/opt/vc/include/interface/vmcs_host/linux)
INCLUDE_DIRECTORIES(/home/mihai/rpi/rootfs/opt/vc/lib)
find_package(OpenCV REQUIRED)
find_package(raspicam REQUIRED)
file(GLOB SOURCE_FILES src/*.cpp)
add_executable(agv_car ${SOURCE_FILES})
target_link_libraries(agv_car ${OpenCv_LIBS} ${raspicam_CV_LIBS})
And here is the tool chain cmake file:
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
# Specify the cross compiler
SET(CMAKE_C_COMPILER $ENV{HOME}/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/mihai/rpi/rootfs/usr/local/lib/cmake)
# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/rpi/rootfs)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
# Search for programs only in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries and headers only in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Any suggestions would be helpful, I have spent some time trying to figure it but without any success. "rpi/rootfs" is the location where I have copied the root file directory of the Raspberry Pi from the SD card.
EDIT
after doing the changes Tsyvarev suggested I get the following errors: `
In file included from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/bits/locale_facets.h:39:0,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/bits/basic_ios.h:37,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/ios:44,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/istream:38,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/sstream:38,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/complex:45,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core/cvstd.inl.hpp:48,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core.hpp:3217,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc.hpp:46,
from /home/mihai/work/src/main.cpp:1:
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cwctype:82:11: error: ‘::wctrans_t’ has not been declared
using ::wctrans_t;
^
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cwctype:104:11: error: ‘::wctrans’ has not been declared
using ::wctrans;
^
In file included from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/imgproc_c.h:46:0,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc.hpp:4698,
from /home/mihai/work/src/main.cpp:1:
/home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/types_c.h: In constructor ‘CvMoments::CvMoments(const cv::Moments&)’:
/home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/types_c.h:423:62: error: call of overloaded ‘sqrt(double&)’ is ambiguous
inv_sqrt_m00 = am00 > DBL_EPSILON ? 1./std::sqrt(am00) : 0;
^
/home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/types_c.h:423:62: note: candidates are:
In file included from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core/cvstd.hpp:65:0,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core/base.hpp:58,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core.hpp:54,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc.hpp:46,
from /home/mihai/work/src/main.cpp:1:
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cmath:482:3: note: float std::sqrt(float)
sqrt(float __x)
^
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cmath:486:3: note: long double std::sqrt(long double)
sqrt(long double __x)
^
CMakeFiles/agv_car.dir/build.make:54: recipe for target 'CMakeFiles/agv_car.dir/src/main.cpp.o' failed
make[2]: *** [CMakeFiles/agv_car.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/agv_car.dir/all' failed
make[1]: *** [CMakeFiles/agv_car.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
`

Linking external libraries cmake arduino

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

cmake link xlib directories c++

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.