catch2 throws and error when adding a struct - c++

This is the root of my project. I think I am missing a basic concept because the error occur when I wrap the the find() function in a struct.
CMakeLists.txt
bst.cpp
bst.hpp
bst-test.cpp
catch.hpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.4 FATAL_ERROR)
project(bst LANGUAGES CXX)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(bst bst.cpp)
add_executable(bst-test bst-test.cpp)
target_link_libraries(bst-test bst)
enable_testing()
add_test(
NAME catch_test
COMMAND $<TARGET_FILE:bst-test> --success
)
bst.cpp
struct Bst {
int find(int num, int array[]) { return -1; }
};
bst.hpp
struct Bst {
int find(int num, int array[]);
};
bst-test.cpp
#include "bst.hpp"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("The number to search is not found in the list", "[notFound]") {
int array[]{};
Bst tree;
REQUIRE(tree.find(1, array) == -1);
}
This is the error when trying to compile.
CMakeFiles/bst-test.dir/bst-test.cpp.o: In function `____C_A_T_C_H____T_E_S_T____0()':
bst-test.cpp:(.text+0x2b0b5): undefined reference to `Bst::find(int, int*)'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/bst-test.dir/build.make:84: recipe for target 'bst-test' failed
make[2]: *** [bst-test] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/bst-test.dir/all' failed
make[1]: *** [CMakeFiles/bst-test.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

you are declaring the Bst structure again in your .cpp file. The source file should only contain the definition of the methods, not the declaration of the structure.
Changing bst.cpp to the following fixes the error:
#include "bst.hpp"
int Bst::find(int num, int array[]) { return -1; }

Related

fatal error: string: No such file or directory

I get this message upon compiling with cmake:
In file included from scanner.lex:6:
C:\Users\uriya\CLionProjects\untitled1\output.hpp:4:10: fatal error: string: No such file or directory
4 | #include <string>
| ^~~~~~~~
compilation terminated.
make.exe[3]: *** [CMakeFiles\untitled1.dir\build.make:88: CMakeFiles/untitled1.dir/lex.yy.c.obj] Error 1
make.exe[3]: *** Waiting for unfinished jobs....
make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/untitled1.dir/all] Error 2
make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/untitled1.dir/rule] Error 2
make.exe: *** [Makefile:117: untitled1] Error 2
This is the part of the code that causes the problems:
#ifndef _236360_2_
#define _236360_2_
#include <string>
using namespace std;
namespace output {
extern const string rules[];
void printProductionRule(int ruleno);
void errorLex(int lineno);
void errorSyn(int lineno);
};
#endif
and that's the cmake input:
cmake_minimum_required(VERSION 3.13)
project(untitled1)
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled1 parser.tab.cpp output.cpp lex.yy.c)
oddly enough I manage to compile it with no problems using g++ -std=c++11 -o hw2 parser.tab.cpp output.cpp lex.yy.c
what's the difference between cmake and this command?

How to fix: CMake-defined macro won't be received by compiler

SOLVED! = instead of - in the CMakeLists.txt in say-hello directory
I have problems with defining a preprocessor variable with CMake according to vector-of-bool's CMake-Tutorial https://www.youtube.com/watch?v=SYgESCQeGJY&list=PLK6MXr8gasrGmIiSuVQXpfFuE1uPT615s&index=8.
Obviously, I am using a different CMake-Version than the one that he's using in the video. But before changing my version I want to tackle the problem here.
After configuring successfully via cmake .., I run make and receive the following:
[ 25%] Building CXX object say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o
<command-line>:0:14: warning: ISO C++11 requires whitespace after the macro name
/home/maximilian/Dokumente/02_Programmieren/VSCMAKE/say-hello/src/say-hello/hello.cpp: In function ‘void hello::say()’:
<command-line>:0:17: error: expected ‘;’ before numeric constant
/home/maximilian/Dokumente/02_Programmieren/VSCMAKE/say-hello/src/say-hello/hello.cpp:7:43: note: in expansion of macro ‘HELLO_VERSION’
std::cout << "Hello (Version " << HELLO_VERSION << ")\n";
^~~~~~~~~~~~~
say-hello/CMakeFiles/say-hello.dir/build.make:62: recipe for target 'say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o' failed
make[2]: *** [say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o] Error 1
CMakeFiles/Makefile2:90: recipe for target 'say-hello/CMakeFiles/say-hello.dir/all' failed
make[1]: *** [say-hello/CMakeFiles/say-hello.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I am using:
Linux
g++ 8.1
C++11
CMake 3.14.4
Visual Studio Code
Top CMakeLists.txt
cmake_minimum_required(VERSION 3.14.4)
project(MyProject VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(say-hello)
add_subdirectory(mainsrc)
CMakeLists.txt in mainsrc
add_executable(main.out main.cpp)
target_link_libraries(main.out PRIVATE say-hello)
main.cpp
#include <iostream>
#include <say-hello/hello.hpp>
int main(){
hello::say();
return 0;
}
CMakeLists.txt in say-hello
add_library(say-hello
src/say-hello/hello.cpp
src/say-hello/hello.hpp
)
target_include_directories(say-hello PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_compile_definitions(say-hello PUBLIC HELLO_VERSION-4)
hello.hpp
#ifndef HELLO
#define HELLO
//#define SAYHELLOVERSION 4
namespace hello{
void say();
}
#endif
hello.cpp
#include <iostream>
#include "hello.hpp"
namespace hello{
void say(){
//int version = SAYHELLOVERSION;
std::cout << "Hello (Version " << HELLO_VERSION << ")\n";
}
}

cmake linking error 2 [duplicate]

This question already has answers here:
Error with multiple definitions of function
(4 answers)
Closed 4 years ago.
I am trying to compile a simple code using cmake and I getting an error. The code and cmake file are as below. The test.cpp is the main file in which i have directly included test1.cpp. I have also included my CMake file and the error that I am getting on performing make.
test.cpp
#ifndef _IOSTREAM_
#include<iostream>
#endif
#include"test1.cpp"
using namespace std;
int main()
{
printing("hello");
return 0;
}
test1.cpp
#ifndef _IOSTREAM_
#include<iostream>
#endif
#include<string>
using namespace std;
void printing(string s)
{
cout<<s<<endl;
return;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
project(test)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11)
add_executable(test test.cpp test1.cpp)
Error
CMakeFiles/test.dir/test1.cpp.o: In function
printing(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >):
/home/vatsal/Desktop/test/test1.cpp:(.text+0x0): multiple definition
of printing(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >)
CMakeFiles/test.dir/test.cpp.o:/home/vatsal/Desktop/test/test.cpp:
(.text+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
CMakeFiles/test.dir/build.make:98: recipe for target test failed
make[2]: *** [test] Error 1
CMakeFiles/Makefile2:67: recipe for target CMakeFiles/test.dir/all
failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target all failed
make: *** [all] Error 2
It happens because include cpp file is a bad idea.
After preprocessor work you'll get two defenition of void printing(string s) The first one is in test.cpp because you've incleded test1.cpp and the second one is in test1.cpp.
Solution is to create test1.h which contain declaration of function:
#include<iostream>
#include<string>
using namespace std;
void printing(string s);
Then fix test1.cpp:
#include "test1.h"
using namespace std;
void printing(string s)
{
cout<<s<<endl;
return;
}
And finaly replace #include"test1.cpp" with #include"test1.h" in test.cpp

/usr/bin/ld: cannot find -lgmock on ubuntu while running gmock program

I am trying to compile simple gmock example on my ubuntu vmware(16.04 LTS)
and getting below error while doing "make"
I have below files -
"test.h"
class CBasicMath
{
public:
CBasicMath(){}
virtual ~CBasicMath() {}
virtual int Addition(int x, int y);
virtual int Multiply(int x, int y);
virtual int Divide(int x, int y);
};
"test.cpp"
#include "test.h"
int CBasicMath::Addition(int x, int y)
{
return (x + y);
}
int CBasicMath::Multiply(int x, int y)
{
return (x * y);
}
int CBasicMath::Divide(int x, int y)
{
return (x / y);
}
"mocktest.h"
#include "gmock/gmock.h"
#include "test.cpp"
class MockBasicTest : public CBasicMath {
public:
MOCK_METHOD2(Addition, int(int x, int y));
MOCK_METHOD2(Multiply, int(int x, int y));
MOCK_METHOD2(Divide, int(int x, int y));
};
"main.cpp"
#include "mocktest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
TEST(BasicMathTest, testAddition) {
MockBasicTest basictest;
EXPECT_CALL(basictest, Addition(2,3)).Times(0);
// EXPECT_EQ(0, basictest.Addition(2,3));
/*
.Times(5);
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
*/
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
"CMakeLists.txt"
cmake_minimum_required(VERSION 2.6)
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests main.cpp)
target_link_libraries(runTests -lgtest -lgmock -lpthread)
These are the steps I followed for compilation -
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ cmake CMakeLists.txt
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ajay/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$
and after that When I did make I am facing the issue
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ make
Scanning dependencies of target runTests
make[2]: Warning: File 'main.cpp' has modification time 84978 s in the future
[ 50%] Building CXX object CMakeFiles/runTests.dir/main.cpp.o
[100%] Linking CXX executable runTests
/usr/bin/ld: cannot find -lgmock
collect2: error: ld returned 1 exit status
CMakeFiles/runTests.dir/build.make:94: recipe for target 'runTests' failed
make[2]: *** [runTests] 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
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$
I don't know why this "/usr/bin/ld: cannot find -lgmock" issue is coming even though I have installed gmock successfully.
I am able to run gtest programs but when I am adding gmock I am getting this issue.
Please help me to resolve.
Let me know for more info.
Lookup the documentation for taget_link_libraries. Check the comments in FindGtest.cmake
You should not specify libraries them with -l instead use the variables from find_package e.g. ${GTEST_LIBRARIES}
You haven't done find_package for GMOCK so there are no variables defined for GMOCK. As this is not a standard CMake module, write your own or take one from the Internet
BUT, the Google test documentation recommends not use the installed libraries from the system, but to build them yourself inside your Project.There are several examples on the internet how to add gtest/gmock as ExternalProject to your cmake project.

xerces c++ and cmake

i tryng to build a small example of Xerces with xerces c++ 3.1 and cmake, but i and only getting linkings problems.
This is my cmkelists.txt:
//============================================================================
project(ConfiguradorXerces)
cmake_minimum_required(VERSION 2.8)
include_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/.libs)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/)
set ( XercesLib xerces-c )
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${XercesLib})
//==============================================
//===============================================
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
using namespace xercesc;
using namespace std;
int main()
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
//==============================================
and this is my console output:
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x25): undefined reference to `xercesc_3_1::XMLUni::fgXercescDefaultLocale'
main.cpp:(.text+0x2a): undefined reference to `xercesc_3_1::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_1::PanicHandler*, xercesc_3_1::MemoryManager*)'
main.cpp:(.text+0x2f): undefined reference to `xercesc_3_1::XMLPlatformUtils::Terminate()'
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_3_1::XMLException'
collect2: error: ld returned 1 exit status
make[2]: *** [ConfiguradorXerces] Error 1
make[1]: *** [CMakeFiles/ConfiguradorXerces.dir/all] Error 2
make: *** [all] Error 2
16:28:55: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ConfiguradorXerces (target: Desktop)
When executing step 'Make'
//
i was thinking trhat my cmakeLsits.txt was not complete, there is an especial setup that it has to be done??
thx in advance
I am pretty sure that target_link_libraries() macro accepts a target as its first parameter:
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
And you have forgot to specify it. So instead of target_link_libraries(${XercesLib}), try target_link_libraries(${PROJECT_NAME} ${XercesLib}).
Hopefully, that solves it.