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

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

Related

Can't cross compile c++ class with sqlite included (undefined reference) on linux (i686-w64-mingw32-g++)

I tried to compile my c++ class with sqltie3 include.
I run Ubuntu 20.04.
For this I use the i686-w64-mingw32-g++ cross compiler, I also tested it with g++ and it works perfectly but not with the i686-w64-mingw32-g++ compiler.
I always get the same error:
/usr/bin/i686-w64-mingw32-ld: DBWrapper.o:DBWrapper.cpp:(.text+0x61): undefined reference to `sqlite3_open'
/usr/bin/i686-w64-mingw32-ld: DBWrapper.o:DBWrapper.cpp:(.text+0x12c): undefined reference to `sqlite3_close'
My Makefile:
mingw = i686-w64-mingw32-g++
SQLCOMPILE = -I/usr/local/sqlite/include
CFLAGS = -Wall
SRCFILES = *.cpp
OBJFILES = *.o
TARGETWIN = progwx.exe
all: $(TARGETWIN)
$(TARGETWIN): $(OBJFILES)
$(mingw) $(CFLAGS) $(OBJFILES) $(SQLCOMPILE) -o $(TARGET) -l sqlite3
$(OBJFILES): $(SRCFILES)
$(mingw) $(CFLAGS) $(SQLCOMPILE) -c $(SRCFILES)
.PHONY: clean
clean:
rm -f *.o
I put the sqlite source code in $(SQLCOMPILE) and the compiler can include everything unless I call the sqlite3_open() function.
I also compiled the source code to a library (libsqlite3.a) so that the -l tag can find it.
Because the g++ compiler brings also the same "undefined reference" error if I don't put the -lsqlite3 tag in.
$ i686-w64-mingw32-gcc -c sqlite3.c
$ ar rcs libsqlite3.a sqlite3.o
Here is my Headerfile (DBWrapper.h):
#pragma once
#include <string>
#include <stdexcept>
#include <sqlite3.h>
class DBWrapper {
sqlite3 *db_;
const int errCode;
public:
DBWrapper(const std::string &dbname);
DBWrapper(const DBWrapper&) = delete;
DBWrapper& operator=(const DBWrapper&) = delete;
sqlite3* operator*();
~DBWrapper();
};
Here is my CPP-File (DBWrapper.cpp):
#include "DBWrapper.h"
DBWrapper::DBWrapper(const std::string &dbname) : db_(nullptr), errCode(sqlite3_open(dbname.c_str(), &db_)){
if(errCode) {
throw std::runtime_error("ERROR at opening database!");
}
}
DBWrapper::~DBWrapper() {
sqlite3_close(db_);
}
sqlite3* DBWrapper::operator *() {
return db_;
}
(sorry for my bad english)
I figured it out I just needed sqlite3 as a compiled object file (sqlite3.o) for the compiler.

C++ Boost Example: Creating and Managing Threads (Compilation Error)

I am currently using Boost 1.54.0. I am following the code from this example.
example_44_01.cpp
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}
void thread()
{
for (int i = 0; i < 5; ++i)
{
wait(1);
std::cout << i << std::endl;
}
}
int main(int argc, char** argv)
{
boost::thread t{thread};
t.join();
return 0;
}
So, it looks like all I need is the -lboost_thread, and -lboost_chrono libraries to link to at compile time. I also added the -lboost_system.
Here are my execution scripts.
g++-7 -Wall -std=c++1z -g -c example_44_01.cpp -o example_44_01.o
g++-7 -Wall -std=c++1z -g example_44_01.o -o example_44_01 -lboost_system -lboost_thread -lboost_chrono &>result.txt
What's going on here? This is the result.txt file:
example_44_01.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:243: undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
I've compiled and linked other programs with the same libraries without error. So is the error in the code? This seems doubtful as the code is straight from the documentation. Any insight is appreciated.
I had this issue once because I was indeliberately using different versions of Boost (I had first installed Boost from commandline, then a few months later on, manually from zip).
Try adding the path to your Boost libraries to the compiler. For instance, if your libraries are stored at /usr/local/lib, try:
g++-7 -Wall -std=c++1z -g example_44_01.o -o example_44_01 -L/usr/local/lib -lboost_system -lboost_thread -lboost_chrono &>result.txt

linux C++. Link shared objects and main

I write simple testing program in C++, which will tell Hello, Alex and exit.
Here it's code:
main.cpp:
#include <iostream>
#include <dlfcn.h>
int main()
{
void* descriptor = dlopen("dll.so", RTLD_LAZY);
std::string (*fun)(const std::string name) = (std::string (*)(const std::string)) dlsym(descriptor, "sayHello");
std::cout << fun("Alex") << std::endl;
dlclose(descriptor);
return 0;
}
dll.h:
#ifndef UNTITLED_DLL_H
#define UNTITLED_DLL_H
#include <string>
std::string sayHello(const std::string name);
#endif
dll.cpp:
#include "dll.h"
std::string sayHello(const std::string name)
{
return ("Hello, " + name);
}
makefile:
build_all : main dll.so
main : main.cpp
$(CXX) -c main.cpp
$(CXX) -o main main.o -ldl
dll.so : dll.h dll.cpp
$(CXX) -c dll.cpp
$(CXX) -shared -o dll dll.o
But when I build my code with make, I have such error:
/usr/bin/ld: dll.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
dll.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
makefile:8: recipe for target 'dll.so' failed
make: *** [dll.so] Error 1
What did I make not correct?
P.S. I use GNU Make 3.81 on Ubuntu Server 14.04.3 with GNU GCC 4.8.4
Update
If I link dll.so file with -fPIC param, I have the same error
Firstly, a bit off topic, but in your makefile, it would be better to specify build_all as a phony target
.PHONY: build_all
Next, you are compiling dll.cpp without relocatable code. You need to add -fpic or -fPIC (see here for an explanation of the difference).
$(CXX) -c dll.cpp -fpic
Lastly, unix doesn't automatically add file suffixes, so here you need to specify .so:
$(CXX) -shared -o dll.so dll.o

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");
}

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

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.