Multiple definition of function, why isn't the guard catching this? - c++

So I wrote a small set of logging functions in the file cerus.h. The contents of that file can be seen below. It is being included in main.cpp, model.cpp, engine.cpp and camera.cpp. As can be seen, I have include guards so I'm not sure why I'm getting this error:
Output of $ make
jed#ArchPC:~/glPlayground$ make
g++ -std=c++11 -c model.cpp -o bin/model.o
g++ -std=c++11 -c tiny_obj_loader.cc -o bin/tinyobj.o
g++ -std=c++11 -c camera.cpp -o bin/camera.o
g++ -g -std=c++11 -o main bin/main.o bin/engine.o bin/tinyobj.o bin/model.o bin/camera.o -lGL -lGLU -lglut -lSOIL -lGLEW -lglfw
bin/engine.o: In function `LOG(char const*)':
engine.cpp:(.text+0x0): multiple definition of `LOG(char const*)'
bin/main.o:main.cpp:(.text+0x0): first defined here
bin/engine.o: In function `LOGERR(char const*)':
engine.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)'
bin/main.o:main.cpp:(.text+0x3d): first defined here
bin/model.o: In function `LOG(char const*)':
model.cpp:(.text+0x0): multiple definition of `LOG(char const*)'
bin/main.o:main.cpp:(.text+0x0): first defined here
bin/model.o: In function `LOGERR(char const*)':
model.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)'
bin/main.o:main.cpp:(.text+0x3d): first defined here
bin/camera.o: In function `LOG(char const*)':
camera.cpp:(.text+0x0): multiple definition of `LOG(char const*)'
bin/main.o:main.cpp:(.text+0x0): first defined here
bin/camera.o: In function `LOGERR(char const*)':
camera.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)'
bin/main.o:main.cpp:(.text+0x3d): first defined here
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'main' failed
make: *** [main] Error 1
cerus.h
#ifndef CERUS_H
#define CERUS_H
#include <iostream>
//Need to add Windows and Mac Includes here
// Linux Include Statements
void LOG(const char* str){
std::cout << "[INFO]" << str << "\n";
}
void LOGERR(const char* str){
std::cout << "[ERROR]" << str << "\n";
}
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GLFW/glfw3.h>
#endif
Makefile
all: main
main: bin/main.o bin/engine.o bin/model.o bin/tinyobj.o bin/camera.o cerus.h
g++ -g -std=c++11 -o main bin/main.o bin/engine.o bin/tinyobj.o bin/model.o bin/camera.o -lGL -lGLU -lglut -lSOIL -lGLEW -lglfw
bin/main.o: main.cpp cerus.h
g++ -std=c++11 -c main.cpp -o bin/main.o
bin/engine.o: engine.cpp engine.h cerus.h
g++ -std=c++11 -c engine.cpp -o bin/engine.o
bin/tinyobj.o: tiny_obj_loader.cc tiny_obj_loader.h cerus.h
g++ -std=c++11 -c tiny_obj_loader.cc -o bin/tinyobj.o
bin/model.o: model.cpp model.h cerus.h
g++ -std=c++11 -c model.cpp -o bin/model.o
bin/camera.o: camera.cpp camera.h cerus.h
g++ -std=c++11 -c camera.cpp -o bin/camera.o
clean:
rm -f bin/*.o main
If someone could explain to me why my guard isn't catching this, I would greatly appreciate the help.
EDIT: Fixed this issue by adding a file called cerus.cpp and defining my logging functions there instead of in cerus.h

This type of guard is to avoid things from being declared or defined in same translation unit.
It won't have any effect for multiple definition in different translation units (i.e. multiple source files).
In this case, you should move the definitions of functions LOG and LOGERR to another .cpp file, and put declarations of the functions in the header file.

Guards did nothing wrong, they just protect your declarations/inlines/templates.
It's the definitions that are real issue. If you have inline functions in your cpp, put them in header, same for templates. Do not include cpp files. Can't see much of your code but that is most of the cases.

Related

How to solve redefinition of main in makefile

I am trying to compile and link three files into an executable with a makefile, but seem to have redefined main or somehow goofed the compile/link process. The project is for a class, where the objective is to implement a linear feedback shift register, but we have to use a makefile.
Where have I redefined main? How do I alter my makefile to create my executable? I notice the error points to test.o as having redefined main, but I'm not sure why or how.
Error:
g++ -c main.cpp LFSR.cpp -Wall -Werror -ansi -pedantic
g++ -c test.cpp -Wall -Werror -ansi -pedantic
g++ main.o LFSR.o test.o -o ps2a -lboost_unit_test_framework
test.o: In function `main':
test.cpp:(.text+0xa3): multiple definition of `main'
main.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'ps2a' failed
make: *** [ps2a] Error 1
My makefile:
all: ps2a
ps2a: main.o LFSR.o test.o
g++ main.o LFSR.o test.o -o ps2a -lboost_unit_test_framework
LFSR.o: LFSR.cpp LFSR.hpp
g++ -c LFSR.cpp -Wall -Werror -ansi -pedantic
main.o: main.cpp LFSR.hpp
g++ -c main.cpp LFSR.cpp -Wall -Werror -ansi -pedantic
test.o: test.cpp
g++ -c test.cpp -Wall -Werror -ansi -pedantic
clean:
rm *.o ps2a
main.cpp:
#include "LFSR.hpp"
int main(){
}
LFSR.hpp
#include <string>
#include <iostream>
class LFSR{
public:
LFSR(std::string, int);
int step();
int generate(int k);
private:
std::string bitString;
int tapPos;
};
LFSR.cpp:
#include "LFSR.hpp"
void makeBitStringValid(std::string& str);
LFSR::LFSR(std::string str, int t){
}
int LFSR::step(){
return 0;
}
int LFSR::generate(int k){
return 0;
}
void makeBitStringValid(std::string& str){
}
test.cpp (Note, this is given by the instructor-- I'm not entirely sure how it works yet)
#include <iostream>
#include <string>
#include "LFSR.hpp"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(fiveBitsTapAtTwo) {
LFSR l("00111", 2);
BOOST_REQUIRE(l.step() == 1);
BOOST_REQUIRE(l.step() == 1);
BOOST_REQUIRE(l.step() == 0);
BOOST_REQUIRE(l.step() == 0);
BOOST_REQUIRE(l.step() == 0);
BOOST_REQUIRE(l.step() == 1);
BOOST_REQUIRE(l.step() == 1);
BOOST_REQUIRE(l.step() == 0);
LFSR l2("00111", 2);
BOOST_REQUIRE(l2.generate(8) == 198);
}
Do not provide own main because The Boost Unit Test Framework already provides one in your test.cpp with the lines:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
Dynamic library variant of the UTF
Unlike the static library variant function main() can't reside in the
dynamic library body. Instead this variant supplies default function
main() implementation as part of the header boost/test/unit_test.hpp
to be generated as part of your test file body. The function main() is
generated only if either the BOOST_TEST_MAIN or the BOOST_TEST_MODULE
flags are defined during a test module compilation. For single-file
test module flags can be defined either in a test module's makefile or
before the header boost/test/unit_test.hpp inclusion. For a multi-file
test module flags can't be defined in makefile and have to be defined
in only one of the test files to avoid duplicate copies of the
function main().

calling c++ function from c

I need to access a C++ function from C but I get some error like :-
/tmp/ccUqcSZT.o: In function `main':
main.c:(.text+0x5): undefined reference to `load_alert_to_db'
collect2: error: ld returned 1 exit status
My main.c code is:-
#include <stdio.h>
extern void load_alert_to_db(void);
int main(void){
/* Create empty queue */
load_alert_to_db();
return 0;
}
C++ code implementation db_manager.cpp is:-
#include <sstream>
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <time.h>
#include <cstring>
#include <fstream>
//using namespace oracle::occi;
#include <iostream>
using namespace std;
extern "C" void load_alert_to_db(void)
{
cout<<"db occi"<<endl;
}
makefile is:-
CC= g++
all:
$(CC) -c -Wall -Werror -fPIC db_manager.cpp
$(CC) -shared -o libdb_manager.so db_manager.o
gcc -L/home/oracle/Desktop/storage/ -Wall main.c -o data -ldb_manager
gcc -o data main.c
clean:
rm -f *.o data
so please help me which one is my problem. I am also include
export LD_LIBRARY_PATH=/home/oracle/Desktop/storage/:$LD_LIBRARY_PATH
environmental variable in .bash_profile
gcc -o data main.c
Not sure why you have this line in your makefile since it will compile main.c without reference to the previously created library and hence cause an undefined-symbol error such as the one you're seeing.
This is especially so, since you appear to have done it the right way on the preceding line:
gcc -L/home/oracle/Desktop/storage/ -Wall main.c -o data -ldb_manager
However, the entire point of using makefiles is so that it figures out the minimum necessary commands for you, based on dependencies. Lumping a large swathe of commands into a single rule tends to defeat that purpose. You would be better off making your rules a little more targeted, such as (untested but should be close):
all: data
data: main.o libdb_manager.so
gcc -o data main.o -ldb_manager
main.o: main.c
gcc -o main.o main.c
libdb_manager.so: db_manager.cpp
g++ -c -Wall -Werror -fPIC -o db_manager.o db_manager.cpp
g++ -shared -o libdb_manager.so db_manager.o
That way, if you make a small change to one part (like main.c), it doesn't have to go and compile/link everything in your build tree.
Your makefile seems to be completely broken and random, and you're not even linking the required object files. You can simplify this:
all:
$(CC) -c -Wall -Werror -fPIC db_manager.cpp
$(CC) -shared -o libdb_manager.so db_manager.o
gcc -L/home/oracle/Desktop/storage/ -Wall main.c -o data -ldb_manager
gcc -o data main.c
to just this:
all:
gcc -Wall -c main.c
g++ -Wall -c db_manager.cpp
g++ main.o db_manager.o -o data
this is what I needed to do:
Supposing the C++ function is called Debug::Write(str)
Then in your hpp file do the following:
#ifdef __cplusplus
extern "C" void DebugTmp(char *str);
#endif
Then in the corresponding cpp file do this:
void DebugTmp(char *str)
{
Debug::Write(str);
}
Then in your C file where you call DebugTmp define the prototype:
void DebugTmp(char *str);
then call it as below:
static void MyFunction( void )
{
DebugTmp("This is debug trace\n");
}

C++ BOOST undefined reference to `boost::filesystem::detail::copy_file

I have no clue why boost::filesystem::copy_file is making trouble for me.
undefined reference to `boost::filesystem::detail::copy_file
// g++ -std=c++11 test.cpp -lboost_filesystem -lboost_system -lrt -lboost_wave
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::create_directory("aaa");
// ok
boost::filesystem::copy_file("f1","f2");
// /tmp/ccNWZltB.o: In function `boost::filesystem::copy_file(boost::filesystem::path const&, boost::filesystem::path const&)':
// test.cpp:(.text._ZN5boost10filesystem9copy_fileERKNS0_4pathES3_[_ZN5boost10filesystem9copy_fileERKNS0_4pathES3_]+0x26): undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
// collect2: error: ld returned 1 exit status
return 0;
}
I got no inspiration from the source code of boost or its help:
inline
void copy_file(const path& from, const path& to, // See ticket #2925
BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
{detail::copy_file(from, to, option, &ec);}
Even such a simple example does not work for me.
Platform: Linux Ubuntu 64
There is a workaround for this problem, replace
#include <boost/filesystem.hpp>
with
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
Or, preferably, add -DBOOST_NO_CXX11_SCOPED_ENUMS to your compiler flags
If you run into this problem make sure to include both -lboost_system and -lboost_filesystem in your call to g++
Example working Makefile
BINARY = output
FILE_OBJECTS = main.o fileLoader.o
BOOST = -lboost_system -lboost_filesystem
GCC = g++ -std=c++17
FLAGS = -Wall -pedantic -Wextra
build: $(FILE_OBJECTS)
$(GCC) $(FLAGS) $(FILE_OBJECTS) -o $(BINARY) $(BOOST)
main.o: main.cpp fileLoader.o
$(GCC) $(FLAGS) -c main.cpp
fileLoader.o: fileLoader.cpp
$(GCC) $(FLAGS) -c fileLoader.cpp
clean:
rm -rf *.o $(BINARY)
Example working code
#include <boost/filesystem.hpp>
void create_data_file(std::string file_path)
{
boost::filesystem::path p(file_path);
boost::filesystem::create_directory(p);
}
I could not compile a file that included the header boost/filesystem.hpp either. This is how I solved it: I commented out the line boost/filesystem.hpp and all the lines that were using Boost, and then compiled the file. I then uncommented all the lines in the files and compiled again, and then it worked. I was compiling with the flag -lboost_system both times!
In older boost versions it is BOOST_NO_SCOPED_ENUMS, not BOOST_NO_CXX11_SCOPED_ENUMS
see boost::filesystem::copy_file() missing symbol in c++11

scorep qt error undefined reference to `POMP2_Assign_handle'

Trying to connect simplest OpenMP QT project with score-P:
#include <QTextStream>
#include <QDateTime>
#include <QProcess>
#include <QFile>
#include <omp.h>
int main()
{
omp_set_num_threads(200);
#pragma omp parallel for
for(int i = 0; i < 200; i ++)
{
QFile file(QString("test_file_%1").arg(i));
if(file.open(QIODevice::Append))
{
QTextStream stream(&file);
stream << QDateTime::currentDateTime().toString(Qt::ISODate) + "\n";
file.close();
}
}
}
Make some changes in *.pro:
QMAKE_CXXFLAGS += -g -fopenmp
QMAKE_LIBS += -lgomp -lpthread
QMAKE_CXX = /home/monika/scorep/bin/scorep g++
QMAKE_LINK = /home/monika/scorep/bin/scorep g++
And cathing LINK error:
/home/monika/scorep/bin/scorep g++ -c -m64 -pipe -g -fopenmp -O2 -g -pipe -Wall -Wp,- D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_CORE_LIB -I/usr/lib64/qt4/mkspecs/linux-g++-64 -I../test-scorep -I/usr/include/QtCore -I/usr/include -I../../scorep/include/opari2 -I. -I../test-scorep -I. -o main.o ../test-scorep/main.cpp
/home/monika/scorep/bin/scorep g++ -m64 -Wl,-O1 -o test-scorep main.o -lgomp -lQtCore -lpthread
main.o: In function `POMP2_Init_reg_bl9o0ezmjm8_1':
/home/monika/test/build-test-scorep-Desktop-Release/main.prep.cpp.opari.inc:15: undefined reference to `POMP2_Assign_handle'
main.o: In function `main.omp_fn.0':
/home/monika/test/test-scorep/main.cpp:10: undefined reference to `pomp_tpd_'
/home/monika/test/test-scorep/main.cpp:11: undefined reference to `POMP2_Parallel_begin'
/home/monika/test/test-scorep/main.cpp:12: undefined reference to `POMP2_For_enter'
/home/monika/test/test-scorep/main.cpp:22: undefined reference to `POMP2_Implicit_barrier_enter'
/home/monika/test/test-scorep/main.cpp:24: undefined reference to `POMP2_Implicit_barrier_exit'
/home/monika/test/test-scorep/main.cpp:25: undefined reference to `POMP2_For_exit'
/home/monika/test/test-scorep/main.cpp:27: undefined reference to `POMP2_Parallel_end'
main.o: In function `main':
/home/monika/test/test-scorep/main.cpp:14: undefined reference to `POMP2_Parallel_fork'
/home/monika/test/test-scorep/main.cpp:10: undefined reference to `pomp_tpd_'
/home/monika/test/test-scorep/main.cpp:28: undefined reference to `POMP2_Parallel_join'
If i compile simple project (with only one file) like:
/home/monika/scorep/bin/scorep g++ main.cpp
Everything work (score-P too). If I change QMAKE_CXX or QMAKE_LINK to default (g++) bulding will be well, but score-P will not work.
I tried to include pomp2_lib.h (which contains functions from unfounded list), but still no result (nothing changed).

external print function how compile

I created two Files where the second File should print a std::vector.
That's my main.cpp:
#include <vector>
int main() {
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
return 0;
}
That's the print.cpp
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
template <typename T> void printVec(T vec) {
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
template void printVec< std::vector<int> >(std::vector<int> t);
I compiled those files with:
g++ -g main.cpp -o main.o
g++ -g -c print.cpp -o print.o
g++ -o main main.o print.o
and get following error
main.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
main.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
main.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
main.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
main.o: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
main.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
main.o:(.dtors+0x8): first defined here
/usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
How am I supposed to get it to run?
Change:
g++ -g main.cpp -o main.o
g++ -g -c print.cpp -o print.o
g++ -o main main.o print.o
to:
g++ -g -c main.cpp -o main.o
g++ -g -c print.cpp -o print.o
g++ -o main main.o print.o
Or alternatively you can just combine all 3 operations into one (and enable warnings while you're at it):
g++ -g -Wall -o main main.cpp print.cpp