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
Related
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?
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; }
i have included the path to python.h(C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include) file and gave the library path of the project to "C:\Users\AppData\Local\Programs\Python\Python37-32\Lib"
and my code is as pasted below:-
#include <iostream>
#include <Python.h>
using namespace std;
int main(int argc, char **argv)
{
PyObject *pInt;
Py_Initialize();
PyRun_SimpleString("print('Hello world from embedded python!!!\n')");
Py_Finalize();
cout<<"now i am finally out of the python"<<endl;
return 0;
}
when i compile the program there is no error but when i build the project i get the following erros
C:/Users/Documents/CppWorkspace/Cpp_in_python/main.cpp:8: undefined reference to `_imp__Py_Initialize'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Documents/CppWorkspace/Cpp_in_python/main.cpp:9: undefined reference to `_imp__PyRun_SimpleStringFlags'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Documents/CppWorkspace/Cpp_in_python/main.cpp:10: undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/Cpp_in_python] Error 1
mingw32-make.exe: *** [All] Error 2
Cpp_in_python.mk:78: recipe for target 'Debug/Cpp_in_python' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/vinay/Documents/CppWorkspace/Cpp_in_python'
Makefile:4: recipe for target 'All' failed
====4 errors, 0 warnings====
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";
}
}
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.