How to manage importing library using CMake in c++ error - c++

I want to import spdlog in my project using CMake. And I use VS Code.
But my code doesn't work well. I think I don't have any problem in my code because I copy and paste all professor's code.
I think that it is simple setting error, but I don't know the solution.
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
set(PROJECT_NAME cmake_project_example)
set(CMAKE_CXX_STANDARD 17)
project(${PROJECT_NAME})
add_executable(${PROJECT_NAME} src/main.cpp)
include(ExternalProject)
set(DEP_INSTALL_DIR ${PROJECT_BINARY_DIR}/install)
set(DEP_INCLUDE_DIR ${DEP_INSTALL_DIR}/include)
set(DEP_LIB_DIR ${DEP_INSTALL_DIR}/lib)
ExternalProject_Add(
dep-spdlog
GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
GIT_TAG "v1.x"
GIT_SHALLOW 1
UPDATE_COMMAND ""
PATCH_COMMAND ""
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${DEP_INSTALL_DIR}
TEST_COMMAND ""
)
set(DEP_LIST ${DEP_LIST} dep-spdlog)
set(DEP_LIBS ${DEP_LIBS} spdlog$<$<CONFIG:Debug>:d>)
target_include_directories(${PROJECT_NAME} PUBLIC ${DEP_INCLUDE_DIR})
target_link_directories(${PROJECT_NAME} PUBLIC ${DEP_LIB_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC ${DEP_LIBS})
add_dependencies(${PROJECT_NAME} ${DEP_LIST})
main.cpp
#include <spdlog/spdlog.h>
int main(int argc, const char** argv) {
SPDLOG_INFO("Hello, world!");
return 0;
}
Heres the output:
build Output
[proc] Executing command: D:\Program\CMake\bin\cmake.EXE --build d:/Project/real_cmake/build --config Debug --target all -j 26 --
[build] [ 9%] Performing build step for 'dep-spdlog'
[build] [ 80%] Built target spdlog
[build] [ 90%] Building CXX object example/CMakeFiles/example.dir/example.cpp.obj
[build] In file included from D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/sinks/udp_sink.h:10,
[build] from D:\Project\real_cmake\build\dep-spdlog-prefix\src\dep-spdlog\example\example.cpp:243:
[build] D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/details/udp_client-windows.h: In constructor 'spdlog::details::udp_client::udp_client(const string&, uint16_t)':
# **[build] D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/details/udp_client-windows.h:67:13: error: 'InetPtonA' was not declared in this scope <<<<<< I think this error is critical
# **[build] if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
[build] ^~~~~~~~~
[build] D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/details/udp_client-windows.h:67:13: note: suggested alternative: 'GetPropA'
[build] if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
[build] ^~~~~~~~~
[build] GetPropA
[build] mingw32-make.exe[5]: *** [example\CMakeFiles\example.dir\build.make:76: example/CMakeFiles/example.dir/example.cpp.obj] Error 1
[build] mingw32-make.exe[4]: *** [CMakeFiles\Makefile2:125: example/CMakeFiles/example.dir/all] Error 2
[build] mingw32-make.exe[3]: *** [Makefile:155: all] Error 2
[build] mingw32-make.exe[2]: *** [CMakeFiles\dep-spdlog.dir\build.make:86: dep-spdlog-prefix/src/dep-spdlog-stamp/dep-spdlog-build] Error 2
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:110: CMakeFiles/dep-spdlog.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:90: all] Error 2
[proc] The command: D:\Program\CMake\bin\cmake.EXE --build d:/Project/real_cmake/build --config Debug --target all -j 26 -- exited with code: 2
[build] Build finished with exit code 2
I tried searching the error on internet, but I can't find any information for this problem. I don't have any knowledge about CMake and spdlog.

Related

OpenCV with CMake not working, unsure why

Trying to get OpenCV included in my C++ project, but having issues for some reason...
I followed the instructions to install from source:
git clone https://github.com/opencv/opencv.git
mkdir build && cd build
cmake ../opencv
make -j4
make install
I can see it in my /usr/local/include under 'opencv4'
In my CMakeLists.txt I have:
add_executable(Nssams main.cpp ${BACKWARD_ENABLE})
add_subdirectory(camera)
add_subdirectory(mqtt_client)
add_subdirectory(jsoncpp)
add_subdirectory(serial)
add_subdirectory(backward-cpp)
if(${BUILD_WITH_TESTING})
add_subdirectory(tests)
endif()
find_package(PahoMqttCpp REQUIRED)
find_package(spdlog REQUIRED)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV LIBS = ${OpenCV_LIBS}")
target_link_libraries(Nssams
mqtt
camera
jsoncpp
serial
PahoMqttCpp::paho-mqttpp3-static
spdlog
${OpenCV_LIBS}
)
The messages are spitting out:
[cmake] -- OpenCV_INCLUDE_DIRS = /usr/local/include/opencv4
[cmake] -- OpenCV LIBS = opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;
But when I try to #include, i.e.
#include "opencv4/opencv2/core.hpp"
It's red underlined and the error says
#include errors detected based on information provided by the configurationProvider setting. Squiggles are disabled for this translation unit (/home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/src/camera/ebcmos.cpp).C/C++(1696)
cannot open source file "opencv2/core/cvdef.h" (dependency of "opencv4/opencv2/core.hpp")C/C++(1696)
Where am I going wrong?
I have done very similar things with the spdlog library that's in the CMakeLists.txt with no issues. Just built it from source, added it to the CMakeLists.txt and could use without issues
EDIT:
CMakeLists.txt build output while having #include "opencv4/opencv2/core/core.hpp" included in a .cpp file
[main] Building folder: nssams-cpp-ebcmos
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/build --config Debug --target all -j 4 --
[build] Consolidate compiler generated dependencies of target backward_object
[build] Consolidate compiler generated dependencies of target camera
[build] Consolidate compiler generated dependencies of target jsoncpp
[build] Consolidate compiler generated dependencies of target mqtt
[build] [ 6%] Built target backward_object
[build] [ 12%] Building CXX object src/camera/CMakeFiles/camera.dir/ebcmos.cpp.o
[build] [ 31%] Built target mqtt
[build] [ 43%] Built target jsoncpp
[build] Consolidate compiler generated dependencies of target serial
[build] Consolidate compiler generated dependencies of target backward
[build] [ 56%] Built target backward
[build] [ 68%] Built target serial
[build] In file included from /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/src/camera/ebcmos.cpp:8:
[build] /usr/local/include/opencv4/opencv2/core/core.hpp:48:10: fatal error: opencv2/core.hpp: No such file or directory
[build] 48 | #include "opencv2/core.hpp"
[build] | ^~~~~~~~~~~~~~~~~~
[build] compilation terminated.
[build] gmake[2]: *** [src/camera/CMakeFiles/camera.dir/build.make:104: src/camera/CMakeFiles/camera.dir/ebcmos.cpp.o] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:219: src/camera/CMakeFiles/camera.dir/all] Error 2
[build] gmake: *** [Makefile:136: all] Error 2
[proc] The command: /usr/bin/cmake --build /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/build --config Debug --target all -j 4 -- exited with code: 2 and signal: null
[build] Build finished with exit code 2
It does build correctly if I do not have that include defined
output below:
[main] Building folder: nssams-cpp-ebcmos
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/build --config Debug --target all -j 4 --
[build] [ 6%] Building CXX object src/camera/CMakeFiles/camera.dir/ebcmos.cpp.o
[build] [ 12%] Built target backward_object
[build] [ 25%] Built target jsoncpp
[build] [ 43%] Built target mqtt
[build] [ 56%] Built target serial
[build] [ 68%] Built target backward
[build] [ 75%] Linking CXX static library ../../lib/libcamera.a
[build] [ 87%] Built target camera
[build] Consolidate compiler generated dependencies of target Nssams
[build] [ 93%] Linking CXX executable ../bin/Nssams
[build] [100%] Built target Nssams
[build] Build finished with exit code 0

Installing Library Clion (SDL)

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)

Mixing C++ and C/C++ static library with CMake

I have searched various forums and cannot find a solution to my problem.
I'm trying to write unit tests of a function in a C file. I'm using google test library.
Although I follow the guides, the project doesn't compile properly.
Tests written in C ++ do not see functions in C file. (undefined reference)
Below I am attaching my files, maybe someone will notice where I made a mistake. I'm out of ideas.
pir_driver.h
#ifndef PIR_DRIVER_H_
#define PIR_DRIVER_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
uint32_t xTaskGetTickCount();
#ifdef __cplusplus
} /* extern "C" */
#endif
pir_driver.c
#include "pir_driver.h"
uint32_t xTaskGetTickCount()
{
return 1000;
}
pir_test.cpp
#include <gtest/gtest.h>
#include "../pir_driver.h"
TEST(PIR_Timer_Test, Start)
{
uint32_t test = xTaskGetTickCount() * 2;
EXPECT_EQ(test, test);
}
Project root CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(This pir_driver)
project(${This} C CXX)
include(Dart)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
add_subdirectory(googletest)
set(Headers
pir_driver.h
)
set(Source
pir_driver.c
)
add_library(${This} STATIC ${Sources} ${Headers})
set_target_properties(${This} PROPERTIES LINKER_LANGUAGE C)
add_subdirectory(test)
test CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(This pir_test)
set(Sources pir_test.cpp )
add_executable(${This} ${Sources})
set_target_properties(${This} PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(${This} PUBLIC pir_driver gtest_main )
add_test( NAME ${This} COMMAND ${This})
Compile log:
[proc] Wykonywanie polecenia: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/xxx/ansi_c_projects/pir_test/build --config Debug --target all -- -j 10
[build] [ 14%] Linking C static library libpir_driver.a
[build] [ 28%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
[build] [ 28%] Built target pir_driver
[build] [ 42%] Linking CXX static library ..\lib\libgtestd.a
[build] [ 42%] Built target gtest
[build] [ 57%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj
[build] [ 71%] Linking CXX static library ..\lib\libgtest_maind.a
[build] [ 71%] Built target gtest_main
[build] [ 85%] Building CXX object test/CMakeFiles/pir_test.dir/pir_test.cpp.obj
[build] [100%] Linking CXX executable pir_test.exe
[build] CMakeFiles\pir_test.dir/objects.a(pir_test.cpp.obj): In function `PIR_Timer_Test_Start_Test::TestBody()':
[build] C:/Users/xxx/ansi_c_projects/pir_test/test/pir_test.cpp:13: undefined reference to `xTaskGetTickCount'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [test\CMakeFiles\pir_test.dir\build.make:108: test/pir_test.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:1004: test/CMakeFiles/pir_test.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:113: all] Error 2
The reason it doesn't work is that you have a typo in the main CMakeLists.txt: You define the variable Source, but use the variable Sources when populating the sources of the pir_driver target. Consequently the .c file isn't compiled and the linker can't find the symbol defined within.
The missing .c file is also the reason why you needed to manually set the linker language in the first place. Once you add the source file you can remove the set_target_properties(${This} PROPERTIES LINKER_LANGUAGE C) line as CMake will figure it out itself based on the extensions of the source files.
To avoid such problems in the future you can use
add_library(pir_driver STATIC)
target_sources(pir_driver
PRIVATE
pir_driver.h
pir_driver.c
)
instead of CMake variables to collect sources and headers in CMake 3.11 and later.

GTest Based Test Binary Fails at Build

I am gonna integrate GoogleTest into my project but am facing the some troubles to make my test binary built.
As my code to test is C++20 I need to use g++-10 to build it.
In my root CMakeLists.txt I have the following definition:
...
include( CTest )
# Find Google Test package
find_package( GTest REQUIRED )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/test )
Within the test/ subdirectory there is CMakeLists.txt file containing:
project( ReVolta-Kernel-Tests VERSION 0.1.0 LANGUAGES CXX C )
set( TEST_SAMPLE_TARGET "sample" )
add_executable( ${TEST_SAMPLE_TARGET} )
target_sources( ${TEST_SAMPLE_TARGET}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/test_sample.cpp
)
target_include_directories( ${TEST_SAMPLE_TARGET}
PUBLIC
# Google Test inclusions
${GTEST_INCLUDE_DIRS}
)
target_link_libraries( ${TEST_SAMPLE_TARGET}
PUBLIC
${GTEST_LIBRARIES}
pthread
)
And the content of my test_sample.cpp is just:
#include <gtest/gtest.h>
#if true
TEST( Sample, FirstTest ) {
}
#endif
int main( int argc, char ** argv ) {
testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}
Once I try to make it built (CMake configuration succeeds), there is the following error, related to g++-10 somehow:
[build] Scanning dependencies of target test_sample.cpp
[build] [ 50%] Building CXX object test/kernel/CMakeFiles/test_sample.dir/test_sample.o
[build] In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/c++config.h:518,
[build] from /usr/include/c++/10/limits:42,
[build] from /usr/local/include/gtest/gtest.h:55,
[build] from /home/martin/git/revolta/test/kernel/test_sample.cpp:1:
[build] /usr/include/x86_64-linux-gnu/c++/10/bits/os_defines.h:39:10: fatal error: features.h: File or directory does not exist
[build] 39 | #include <features.h>
[build] | ^~~~~~~~~~~~
[build] compilation terminated.
[build] make[3]: *** [test/kernel/CMakeFiles/test_sample.dir/build.make:82: test/kernel/CMakeFiles/test_sample.dir/test_sample.o] Chyba 1
[build] make[2]: *** [CMakeFiles/Makefile2:897: test/kernel/CMakeFiles/test_sample.dir/all] Chyba 2
[build] make[1]: *** [CMakeFiles/Makefile2:904: test/kernel/CMakeFiles/test_sample.dir/rule] Chyba 2
[build] make: *** [Makefile:512: test_sample] Chyba 2
Does anyone have an idea, at least, what might be wrong? Something with my toolchain? libc?
Thanks to anyone willing to help or push me the right direction! Martin

How do you link SDL2 using cmake on clion?

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.