I have created a simple C++ application. I can compile it, and it works fine. But now I need to load the library dynamically, and I have added dlfnc.h to my project and added some more code:
#include <iostream>
#include <dlfcn.h>
void *mylib;
int eret;
using namespace std;
int main() {
mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
eret = dlclose(mylib);
cout << "!!!Hello, World!!!" << endl; // Prints !!!Hello, World!!!
return 0;
}
Compiling:
cd ~/workspace/LinuxGcc/src
g++ LinuxGcc.cpp
And I got a compilation error:
/tmp/ccxTLiGY.o: In function `main':
LinuxGcc.cpp:(.text+0xf): undefined reference to `dlopen'
LinuxGcc.cpp:(.text+0x25): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
dlfcn.h exist in /usr/include/.
Where is the problem?
From dlopen(3):
Link with -ldl.
so
g++ LinuxGcc.cpp -ldl
will be OK.
The solution is very simple. Add the -ldl flag for linking.
In case of the Bazel build system, linkopts = ['-ldl'].
Related
I want to use pacman to install OpenGL but I get an error compiling.
I want to link with -lglut -lGLU -lGL but what packages are those? Is there a way to find out?
#include <iostream>
using namespace std;
#include <SDL2\SDL.h>
#include <GL\glu.h>
int main(int arc, char *argv[]) {
cout << "Hello world" << endl;
glShadeModel(GL_SMOOTH);
return 0;
}
I run (obviously I get an error saying the lib is not found if using -lGLU)
g++ *.cpp
This is the error
C:\Users\User\AppData\Local\Temp\ccztMdnY.o:main.cpp:(.text+0x39): undefined reference to `__imp_glShadeModel'
F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
I've built Boost 1.59 for MinGW 4.9.2 32bit as follows:
bootstrap.bat mingw
b2 --prefix=%USERPROFILE%\Code\Libraries\boost toolset=gcc install (these are static libraries)
When trying to compile:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
cout << "Hello World!" << endl;
return 0;
}
With:
g++ -o test -L %USERPROFILE%\Code\Libraries\boost\lib -l boost_system-mgw49-mt-1_59 -I %USERPROFILE%\Code\Libraries\boost\include\boost-1_59 main.cpp
I get this error:
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x193): undefined reference to `boost::system::generic_category()'
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x19d): undefined reference to `boost::system::generic_category()'
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x1a7): undefined reference to `boost::system::system_category()'
C:/Program Files (x86)/QT/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o: bad reloc address 0xe in section `.text$_ZN5boost6system14error_categoryD2Ev[__ZN5boost6system14error_categoryD2Ev]'
collect2.exe: error: ld returned 1 exit status
I'm getting this same error across 2 computers. Any help is appreciated!
It's a linker error. You need to link to the Boost System library.
Do this
g++ -o test -L %USERPROFILE%\Code\Libraries\boost\lib -lboost_system -I %USERPROFILE%\Code\Libraries\boost\include\boost-1_59 main.cpp
The problem I'm facing is difficult to describe and explain, but let's try...
Enrivornment: Ubuntu, C++ and g++
So I have an hierarchy of c++ projects and namespaces:
main: My main program - that calls....
objectaccess: Access objects (read, write, update, delete) - that calls...
commonaccess: Encapsulate access to sqlite3 functions (sqlite3_open, sqlite3_exec, etc.) - that calls...
sqlite3.so: The Sqlite3 library.
So, an example call code would be:
#include "objectaccess.hpp"
#include "commonaccess.hpp"
int main()
{
int id = 10;
myobjecttype mo = objectaccess::get(id);
}
At objectaccess I have:
#include "commonaccess.hpp"
namespace objectaccess {
myobjecttype get(int id)
{
myobjecttype mo = commonaccess::getFromTableX(id);
return mo;
}
}
At commonaccess I have:
#include <sqlite3.h>
namespace commonaccess {
myobjecttype getFromTableX(int id)
{
sqlite3_init(whatever...);
sqlite3_exe(whatever...);
myobjecttype retobject;
retobject.whatever = data1;
return retobject;
}
}
The code runs fine and is tested, except for one problem. All three namespaces are in different projects delivering a dynamic library (commonaccess.so and objectaccess.so) except for the main program that returns a binary executable.
My problem is:
At main, if I make a call to any of commonaccess functions, I get the following errors on linking:
g++ -L"/home/workspace/objectaccess/Debug"
-L"/home/workspace/commonaccess/Debug"
-L/usr/lib/i386-linux-gnu -Xlinker -rpath="./" -o "test" ./src/test.o
-lsqlite3 -lobjectaccess -lcommonaccess
/home/workspace/commonaccess/Debug/libcommonaccess.so: undefined reference to `sqlite3_close'
/home/workspace/commonaccess/Debug/libcommonaccess.so: undefined reference to `sqlite3_exec'
/home/workspace/commonaccess/Debug/libcommonaccess.so: undefined reference to `sqlite3_free'
/home/workspace/commonaccess/Debug/libcommonaccess.so: undefined reference to `sqlite3_errmsg'
/home/workspace/commonaccess/Debug/libcommonaccess.so: undefined reference to `sqlite3_open'
/home/workspace/commonaccess/Debug/libcommonaccess.so: undefined reference to `sqlite3_last_insert_rowid'
collect2: error: ld returned 1 exit status
This is simply solved adding at main() a call to any function of commonaccess, like:
#include "objectaccess.hpp"
#include "commonaccess.hpp"
int main()
{
commonaccess::dummycall();
int id = 10;
myobjecttype mo = objectaccess::get(id);
}
Actually calling a dummy function is not desired, so:
a) Why the linker is not being able to solve these references without directly calling any of the commonaccess functions?
b) Why only adding a call to the lower hierarchy namespace in the main program "teaches" the linker about real reference to sqlite3 functions?
Switch the order of libraries being linked. GNU linker can't reorder them, nor keeps references to calls not used so far, so -lsqlite3 is currently useless. Put it after the library that actually uses sqlite3 calls, something like g++ -L"/home/workspace/objectaccess/Debug" -L"/home/workspace/commonaccess/Debug" -L/usr/lib/i386-linux-gnu -Xlinker -rpath="./" -o "test" ./src/test.o -lobjectaccess -lcommonaccess -lsqlite3
I am trying to define a macro for the source file from the command line on an ubuntu system using the -D flag .
The source file is:
#include<iostream>
using namespace std;
int factorial(int n){
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
#ifdef DEEPAK
cout<<"hello"<<endl;
#endif
}
int main()
{
factorial(4);
return(0);
}
The command I am typing is:
gcc -Wall -DDEEPAK factorial.cpp -o main
BUT, I am getting the error:
/tmp/cc4Ii5l2.o: In function `__static_initialization_and_destruction_0(int, int)':
factorial.cpp:(.text+0x63): undefined reference to `std::ios_base::Init::Init()'
factorial.cpp:(.text+0x72): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Any help would be deeply appreciated. Thanks.
You should use g++ instead of gcc in the command, because gcc doesn't link to the C++ STL by default, and hence it gives an undefined reference to std::ios_base.
g++ -Wall -DDEEPAK factorial.cpp -o main
Here's my code. I'm just testing Boost::process so I'll be able to use it if/when I need to. I don't know why I'm getting the linking error that I am getting. I'm a rather novice C++ programmer. I know the concepts, but I make frequent errors in practice and am bad at debugging. I appreciate any help I can get with this.
#include<iostream>
#include<boost/process.hpp>
#include<boost/iostreams/device/file_descriptor.hpp>
namespace bp = ::boost::process;
namespace bpi = ::boost::process::initializers;
namespace bio = ::boost::iostreams;
int main(int argc, char *argv[])
{
bp::pipe p = bp::create_pipe();
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
bp::execute(
bpi::run_exe("/usr/bin/ls"),
bpi::bind_stdout(sink)
);
return(0);
}
And here is my error…
/tmp/cc7cmrV8.o: In function `main':
test.cpp:(.text+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(int, boost::iostreams::file_descriptor_flags)'
/tmp/cc7cmrV8.o: In function `boost::process::posix::initializers::bind_stdout::bind_stdout(boost::iostreams::file_descriptor_sink const&)':
test.cpp:(.text._ZN5boost7process5posix12initializers11bind_stdoutC2ERKNS_9iostreams20file_descriptor_sinkE[_ZN5boost7process5posix12initializers11bind_stdoutC5ERKNS_9iostreams20file_descriptor_sinkE]+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
/tmp/cc7cmrV8.o: In function `void boost::process::posix::initializers::bind_stdout::on_exec_setup<boost::process::posix::executor>(boost::process::posix::executor&) const':
test.cpp:(.text._ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_[_ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_]+0x18): undefined reference to `boost::iostreams::file_descriptor::handle() const'
collect2: error: ld returned 1 exit status
Platform: Linux 64-bit
Boost: 1.55 (installed via pacman)
Boost::process: 0.5
Compile command: g++ -Wall test.cpp -o spegh.elf -lboost_system
A simple search threw me at -This-.
Seeing you posted your compile command, I'm guessing you are simply missing -lboost_iostreams in your linker settings.