build with gtest failed - c++

OS:x64 win7 sp1 with vs2017
I use vcpkg to install gtest, and then try to test it.
I create a dir and it has two files:
//main.cpp
#include <gtest/gtest.h>
int add(int a, int b){
return a + b;
}
TEST(addTest, add){
EXPECT_EQ(1, add(2, -1));
EXPECT_EQ(5, add(2, 3));
}
int main(int argc, char *argv[]){
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TEST();
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(GTest REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main gtest)
Then I use cmake .. "-DCMAKE_TOOLCHAIN_FILE=C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake" and cmake --build ., and here is the log:
C:\src\cmake-test\main.cpp(17): error C3861: “RUN_ALL_TEST”: 找不到标识符(can't find the identifier) [C:\src\cmake-test\build\main.vcxproj]
已完成生成项目“C:\src\cmake-test\build\main.vcxproj”(默认目标)的操作 - 失败。
已完成生成项目“C:\src\cmake-test\build\ALL_BUILD.vcxproj”(默认目标)的操作 - 失败。
生成失败。
“C:\src\cmake-test\build\ALL_BUILD.vcxproj”(默认目标) (1) ->“C:\src\cmake-test\build\main.vcxproj”(默认目标) (3) ->(ClCompile 目标) -> C:\src\cmake-test\main.cpp(17): error C3861: “RUN_ALL_TEST”: 找不到标识符(can't find the identifier) [C:\src\cmake-test\build\main.vcxproj]
0 个警告(warnings)
1 个错误(errors)
已用时间 00:00:01.15
It seems that "RUN_ALL_TEST" is not be defined.
So how to solve the problem?

Related

SDL2 program on windows cannot start (runnerw.exe: CreateProcess failed with error 193: %1)

I downloaded SDL2 release edition from: sdl-github: SDL2-devel-2.24.0-mingw.zip.
The OS is windows 10.
Compile environment is MINGW64.
IDE is clion.
My CMakeLists.txt file is
cmake_minimum_required(VERSION 3.20)
project(sdl_test)
set(CMAKE_CXX_STANDARD 20)
list(APPEND CMAKE_PREFIX_PATH "D:/lib/SDL2-2.24.0")
MESSAGE(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(sdl2 CONFIG REQUIRED)
MESSAGE(STATUS "SDL2_INCLUDE_DIRS is ${SDL2_INCLUDE_DIRS}")
MESSAGE(STATUS "SDL2_LIBRARIES is ${SDL2_LIBRARIES}")
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(sdl_test main.cpp)
target_link_libraries(sdl_test ${SDL2_LIBRARIES}-static)
target_link_libraries(sdl_test ${SDL2_LIBRARIES}main)
The main.cpp file is:
extern "C"{
#include <SDL.h>
};
#include <stdio.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
printf("SDL initialize ok!");
SDL_Quit();
getchar();
return 0;
}
The program can be compiled with no errors but when running it turns that:
runnerw.exe: CreateProcess failed with error 193: %1

How to mix dynamic and static boost libraries in one cmake project?

I'm trying to link my C++ program with static Boost::program_options and dynamic Boost::log libraries.
Here is my CMakeList.txt file:
project(test)
add_executable(testprog "main.cpp")
add_definitions(-DBOOST_ALL_NO_LIB)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS program_options)
target_link_libraries(testprog PRIVATE Boost::program_options)
set(Boost_USE_STATIC_LIBS OFF)
add_definitions(-DBOOST_LOG_DYN_LINK)
find_package(Boost COMPONENTS log)
target_link_libraries(testprog PRIVATE Boost::log)
and my main.cpp file:
#define BOOST_ALL_NO_LIB
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>
int main(int argc, char* argv[])
{
namespace po = boost::program_options;
po::options_description desc("allowed arguments");
desc.add_options()("log", po::value<std::string>(), "log level");
po::variables_map options;
po::store(po::command_line_parser(argc, argv).options(desc).run(), options);
po::notify(options);
if (options.count("log")) {
std::cout << "log level: " << options["log"].as<std::string>() << std::endl;
}
BOOST_LOG_TRIVIAL(info) << "test message";
return 0;
}
I'm getting boost_filesystem.dll is missing from your computer error.
If I get rid of set(Boost_USE_STATIC_LIBS OFF) line, I'm getting unresolved external symbols boost::log.... Looks like Boost_USE_STATIC_LIBS can be set only once during the configuration time, but I just cannot find the way to tell cmake to split static/dynamic libraries...
Thanks.

Catch2 installation on ubuntu 20.04 #include <catch2/catch.hpp>

I am trying to install Catch2 on ubuntu 20.04.
Used instruction from here.
This is what i do:
$ git clone https://github.com/catchorg/Catch2.git
$ cd Catch2
$ cmake -Bbuild -H. -DBUILD_TESTING=OFF
$ sudo cmake --build build/ --target install
Than it saing me that all ok: link for output.
BUT:
When I try to compile the example: // from here
main.cpp
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch.hpp>
std::uint64_t Fibonacci(std::uint64_t number) {
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
}
TEST_CASE("Fibonacci") {
CHECK(Fibonacci(0) == 1);
// some more asserts..
CHECK(Fibonacci(5) == 8);
// some more asserts..
// now let's benchmark:
BENCHMARK("Fibonacci 20") {
return Fibonacci(20);
};
BENCHMARK("Fibonacci 25") {
return Fibonacci(25);
};
BENCHMARK("Fibonacci 30") {
return Fibonacci(30);
};
BENCHMARK("Fibonacci 35") {
return Fibonacci(35);
};
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(Persistent-world LANGUAGES CXX)
add_executable(${PROJECT_NAME} main.cpp )
find_package(Catch2 REQUIRED)
target_link_libraries(${PROJECT_NAME} Catch2::Catch2)
It output such ERROR: catch2/catch.hpp: No such file or directory
Thanks in advance
The problem is quite simple: clonning catchorg/Catch2 now gets you a v3 branch by default, which works differently. The most important change is that it is no longer single header, and that the catch2/catch.hpp header no longer exists.
You can either switch to the v2 branch before configuring and installing the build, or adapt your code to the changes in v3, starting with this documentation on v2 -> v3 migration.
To get the default main, link against Catch2::Catch2WithMain target.
Admin helped me.
On catch v3. I need:
cmake_minimum_required(VERSION 3.5)
project(Catch2 LANGUAGES CXX)
add_executable(${PROJECT_NAME} main.cpp )
find_package(Catch2)
target_link_libraries(${PROJECT_NAME} Catch2::Catch2WithMain)
If you just link against Catch2::Catch2, you won't get the default main and have to write your own, and your own main needs to invoke the tests. See e.g. that
Than i understand that with main it should looks like:
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch_all.hpp>
std::uint64_t Fibonacci(std::uint64_t number) {
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
}
TEST_CASE("Fibonacci") {
CHECK(Fibonacci(0) == 1);
// some more asserts..
CHECK(Fibonacci(5) == 8);
// some more asserts..
// now let's benchmark:
BENCHMARK("Fibonacci 20") {
return Fibonacci(20);
};
BENCHMARK("Fibonacci 25") {
return Fibonacci(25);
};
BENCHMARK("Fibonacci 30") {
return Fibonacci(30);
};
BENCHMARK("Fibonacci 35") {
return Fibonacci(35);
};
}
int main( int argc, char* argv[] )
{
Catch::Session session; // There must be exactly one instance
// writing to session.configData() here sets defaults
// this is the preferred way to set them
int returnCode = session.applyCommandLine( argc, argv );
if( returnCode != 0 ) // Indicates a command line error
return returnCode;
// writing to session.configData() or session.Config() here
// overrides command line args
// only do this if you know you need to
int numFailed = session.run();
// numFailed is clamped to 255 as some unices only use the lower 8 bits.
// This clamping has already been applied, so just return it here
// You can also do any post run clean-up here
return numFailed;
}

Test framework quit unexpectedly in Clion

Trying to run simple TEST using GTest in Clion.
#include <gtest/gtest.h>
TEST(a, b) { ASSERT_EQ(1, 1); }
int main() { return 0; }
Always getting message "Test framework quit unexpectedly.".
CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(Valera)
set(CMAKE_CXX_STANDARD 20)
add_executable(Valera main.cpp)
add_subdirectory(googletest-master)
include_directories(googletest-master/googletest/include)
include_directories(googletest-master/googlemock/include)
target_link_libraries(Valera gtest gtest_main)

C++: How to properly link compiled object using CMake in VSCode

OS
Windows 10
CMake: 3.16.3
Editor
VSCode: 1.48.1
Extensions
CMake Tools: 1.4.1
C/C++ 0.30.0-insiders3
Kit
Visual Studio Community 2019 Release - amd64
Project Repo
https://gitlab.com/NumeralRocket/kepler Removed, insufficient for minimal reproducible example
Tutorial
Introduction to Google Test and CMake
https://www.youtube.com/watch?v=Lp1ifh9TuFI
I'm attempting to build unit tests for one of my personal projects using CMake, and while I wholly admit I am quite new to CMake and a novice at C++, I am stumped on how to resolve this problem. When I go to build my project I get the following Linker error:
[main] Building folder: kepler
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build n:/Unreal_Engine/Magellan/kepler/build-vscode --config Debug --target ALL_BUILD -- /maxcpucount:14
[build] Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Framework
[build] Copyright (C) Microsoft Corporation. All rights reserved.
[build]
[build] gmock.vcxproj -> N:\Unreal_Engine\Magellan\kepler\build-vscode\lib\Debug\gmockd.lib
[build] gmock_main.vcxproj -> N:\Unreal_Engine\Magellan\kepler\build-vscode\lib\Debug\gmock_maind.lib
[build] kepler.vcxproj -> N:\Unreal_Engine\Magellan\kepler\build-vscode\Debug\kepler.lib
[build] gtest.vcxproj -> N:\Unreal_Engine\Magellan\kepler\build-vscode\lib\Debug\gtestd.lib
[build] gtest_main.vcxproj -> N:\Unreal_Engine\Magellan\kepler\build-vscode\lib\Debug\gtest_maind.lib
[build] LINK : fatal error LNK1104: cannot open file 'Quaternion.lib' [N:\Unreal_Engine\Magellan\kepler\build-vscode\test\QuaternionTests.vcxproj]
[cmakefileapi-parser] Code model version (2.1) of cmake-file-api is unexpected. Expecting (2.0). IntelliSense configuration may be incorrect.
[cmakefileapi-parser] Code model version (2.1) of cmake-file-api is unexpected. Expecting (2.0). IntelliSense configuration may be incorrect.
[build] Build finished with exit code 1
For context, the project is structured as follows:
${ProjectRoot}
├── CMakeLists.txt
├── Quaternion.cpp
├── Quaternion.hpp
├── googletest
└── test
├── CMakeLists.txt
└── QuaternionTest.cpp
${ProjectRoot}/CMakeLists.txt
cmake_minimum_required(VERSION 3.16) # version can be different
set(CMAKE_VERBOSE_MAKEFILE ON)
set(This kepler)
get_filename_component(CODE_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
project(${This}) #name of your project
project(${This} C CXX)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
add_subdirectory(googletest)
add_subdirectory(test)
set(Headers
Quaternion.hpp
)
set(Sources
Quaternion.cpp
)
add_library(${This} STATIC ${Sources} ${Headers})
${ProjectRoot}/Quaternion.cpp
#include <iostream>
#include "Quaternion.hpp"
// Default Constructor
Quaternion::Quaternion() {}
// Specified Value Constructor
Quaternion::Quaternion(double qs, double qi, double qj, double qk) : q0(qs), q1(qi), q2(qj), q3(qk) {}
Quaternion operator + (Quaternion const &quatA, Quaternion const &quatB) // 1) § 5.3
{
Quaternion quatC;
quatC.q0 = quatA.q0 + quatB.q0;
quatC.q1 = quatA.q1 + quatB.q1;
quatC.q2 = quatA.q2 + quatB.q2;
quatC.q3 = quatA.q3 + quatB.q3;
return quatC;
}
Quaternion operator - (Quaternion const &quatA, Quaternion const &quatB) // 1) § 5.3
{
Quaternion quatC;
quatC.q0 = quatA.q0 - quatB.q0;
quatC.q1 = quatA.q1 - quatB.q1;
quatC.q2 = quatA.q2 - quatB.q2;
quatC.q3 = quatA.q3 - quatB.q3;
return quatC;
}
void QuaternionLog(Quaternion quat2log)
{
std::cout << "q0: " << quat2log.q0 << std::endl;
std::cout << "q1: " << quat2log.q1 << std::endl;
std::cout << "q2: " << quat2log.q2 << std::endl;
std::cout << "q3: " << quat2log.q3 << std::endl;
}
int main()
{
Quaternion quat1;
Quaternion quat2(1, 2, 3, 4);
Quaternion quat3 = quat1 + quat2;
Quaternion quat4 = quat1 - quat2;
QuaternionLog(quat1);
QuaternionLog(quat2);
QuaternionLog(quat3);
QuaternionLog(quat4);
}
${ProjectRoot}/Quaternion.hpp
#ifndef QUATERNION_H
#define QUATERNION_H
class Quaternion
{
public:
double q0{ 1.0 };
double q1{ 0.0 };
double q2{ 0.0 };
double q3{ 0.0 };
Quaternion();
Quaternion(double qs, double qi, double qj, double qk);
friend Quaternion operator + (Quaternion const &quatA, Quaternion const &quatB);
friend Quaternion operator - (Quaternion const &quatA, Quaternion const &quatB);
};
#endif /* QUATERNION_H */
${ProjectRoot}/test/CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
set(This QuaternionTests)
set(Sources
QuaternionTest.cpp
)
add_executable(${This} ${Sources})
target_link_libraries(${This} PUBLIC
gtest_main
Quaternion
)
add_test(
NAME ${This}
COMMAND ${This}
)
${ProjectRoot}/test/QuaternionTest.cpp
#include <gtest/gtest.h>
#include "../Quaternion.hpp"
TEST(Quaternion, QuaternionConstructors)
{
Quaternion test_quat_1;
ASSERT_EQ(test_quat_1.q0, 1);
ASSERT_EQ(test_quat_1.q1, 0);
ASSERT_EQ(test_quat_1.q2, 0);
ASSERT_EQ(test_quat_1.q3, 0);
ASSERT_EQ(1,1);
};
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
};
How can I:
Ensure and Inspect that objects are being built appropriately?
Properly instruct CMake such that the linker can find my Quaternion (source code) object?
Any insight would be appreciated.
My mistake was in adding my library incorrectly:
${ProjectRoot}/CMakeLists.txt
cmake_minimum_required(VERSION 3.16) # version can be different
set(CMAKE_VERBOSE_MAKEFILE ON)
set(This kepler)
get_filename_component(CODE_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
project(${This}) #name of your project
project(${This} C CXX)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
add_subdirectory(googletest)
add_subdirectory(test)
set(Headers
Quaternion.hpp
)
set(Sources
Quaternion.cpp
)
add_library(${This} STATIC ${Sources} ${Headers})
I was adding a library named ${THIS} which was "kepler" instead of the expected "Quaternion", so:
add_library(Quaternion STATIC ${Sources} ${Headers})
correctly tells the linker what/where to expect my library
AND
Leaving a main function in Quaternion.cpp, which was removed
Solution Source: vector-of-bool