GTest Based Test Binary Fails at Build - c++

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

Related

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

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.

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

How to generate C++ files for grpc from .proto files on Windows ( cross platform way using CMake)?

I prepare layout for some cross platform project.
One of my CMake files has a def like below:
cmake_minimum_required (VERSION 3.20)
# Protocol buffer
find_package(Protobuf REQUIRED)
find_package(GRPC REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# Protocol buffer files
set(protoFiles
${ProtoDir}/Common/core_types.proto
${ProtoDir}/Cache/data_cache.proto
)
# Private files directory
set(pf "Source")
# Private project files
set(privateFiles
${pf}/CacheSvc.h
${pf}/CacheSvc.cpp
${pf}/main.cpp
)
# Add source files to this project executable.
add_executable (CacheSvc
${privateFiles}
${protoFiles}
)
# Libraries to link with
target_link_libraries(CacheSvc
PRIVATE
Common
DataCache
protobuf::libprotobuf
gRPC::grpc
gRPC::grpc++
)
# protobuf_generate function definition is available under the link below:
# https://github.com/protocolbuffers/protobuf/blob/master/cmake/protobuf-config.cmake.in
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION)
message("grpc_cpp_plugin location is: ${grpc_cpp_plugin_location}")
protobuf_generate(TARGET CacheSvc APPEND_PATH LANGUAGE cpp)
protobuf_generate(TARGET CacheSvc APPEND_PATH LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")
# Installation
install(TARGETS CacheSvc)
It works fine on Linux, but on Windows I receive such errors:
[main] Building folder: Mesh.SaaS
[build] Starting build
[proc] Executing command: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build p:/Mesh.SaaS/out/build/x64-windows --clean-first
[build] [1/1] Cleaning all built files...
[build] FAILED: clean
[build] P:\bin\ninja.exe -t clean
[build] Cleaning... ninja: error: remove(src/Services/CacheSvc/core_typesprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/Services/CacheSvc/data_cacheprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/Services/CalculationSvc/core_typesprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/Services/CalculationSvc/calculationprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/Services/StorageSvc/core_typesprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/Services/StorageSvc/storageprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/ClientTestApps/CacheSvcClient/core_typesprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] ninja: error: remove(src/ClientTestApps/CacheSvcClient/data_cacheprotoc-gen-grpc=P:/Mesh.SaaS/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc/grpc_cpp_plugin.exe): Invalid argument
[build] 0 files.
[build] ninja: build stopped: subcommand failed.
[build] ninja: error: FindFirstFileExA(src/services/cachesvc/core_typesprotoc-gen-grpc=p:/mesh.saas/out/build/x64-windows/vcpkg_installed/x64-windows/tools/grpc): Incorrect file name
A repository with source code showing the issue is here:
https://github.com/stalek71/vs_2019_cmake_issues/tree/grpc_windows_issue
How to make it working cross platform way?

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.

how avoid automatic linking of Qt5 libraries in cmake?

I am intending to compile the following opencv code:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
printf("hello world \n");
return 0;
}
My CMakeLists.txt is the following:
cmake_minimum_required(VERSION 2.8)
find_package( OpenCV REQUIRED )
project( main )
add_executable(main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
When compiled the result is as follows:
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: cannot find -lQt5::Core
/usr/bin/ld: cannot find -lQt5::Gui
/usr/bin/ld: cannot find -lQt5::Widgets
/usr/bin/ld: cannot find -lQt5::Test
/usr/bin/ld: cannot find -lQt5::Concurrent
/usr/bin/ld: cannot find -lQt5::OpenGL
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:143: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I believe that CMake in automatic way include Qt5 libraries, I would want to know how avoid this situation.
Ideology of CMake is to never add something that is not required. It is possible that Qt libs were added to configuration list of CMake.
They might be featured in standard variable you use. You can print content by
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
Configuration file looks OpenCVConfig.cmake.in and may include other files which for some reason were changed.