"DSO missing from command line" when using -nostdlib (g++) - c++

This may be a bit of a weird request but I'm trying to compile some software with g++ with the -nostdlib flag.
I'm pretty new to c++ and I'm still learning how linking and compiling works but I'm trying to compile this code:
#include "../lib/gamesys/types.h"
int main() {
types::setUid(1001);
}
Using these commands:
g++ -Wall -g -c bin/scripttwo.cpp -o bin/scripttwo.o -std=c++14
and
g++ -o bin/scripttwo bin/scripttwo.o software/*.o hardware/*.o *.o -nostdlib -Llib/lib/ -ltypes -ljsoncpp -lreadline -ltypes
and I'm getting the error:
/usr/bin/ld: software/FS.o: undefined reference to symbol '_Unwind_Resume##GCC_3.0'
/usr/bin/ld: /lib64/libgcc_s.so.1: error adding symbols: DSO missing from command line
I'm assuming it has something to do with the stdlib because once I remove -nostdlib, it compiles and links fine.
How can I solve this (it is pretty important that I don't include stdlibs)
Note: If you want to know why I'm trying to avoid stdlibs, let me know, its a long a convoluted story lol

Related

C++ file compiling: -L and -I arguments don't work for boost library

There are similar questions but their answers did not work for my issue.
I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).
To compile correctly, if I understood, I should do the command:
g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe
If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.
Hence I edited my call to g++ by doing:
g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe
But I still get errors of the type undefined reference to boost::unit_test.
I also tried the option -I/usr/include/ which contains the boost folder, without success.
It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.
The right command is thus:
g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
See this answer for a more thorough explanation.
I suggest using a build tool like CMake that takes care of such low-level details for you.

Why am I getting "undefined reference to main"

I am a very new to programming and have a very basic question that may be answered in other threads however I think they are far too advanced for me to understand how. I have actually found many answers so far on this site but this is the first problem that forced me to create an account and ask.
Anyway i am running a very basic example program on linux mint 18.3. Now I have seen this exact code work on a machine with windows 8 I believe so I was wondering if that could be the problem. I have created a class and when i plug in my object then build and run I get:
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o||In function _start':|
(.text+0x20)||undefined reference tomain'|
This is the entire code:
#include <iostream>
#include "Gladius.h"
using namespace std;
int main()
{
Gladius io;
return 0;
}
Thats it very basic. here is the .h
#ifndef GLADIUS_H
#define GLADIUS_H
class Gladius
{
public:
Gladius();
};
#endif // GLADIUS_H
and the .cpp for the class.
#include "Gladius.h"
#include <iostream>
using namespace std;
Gladius::Gladius()
{
cout << "The Gladius is a short sword" << endl;
}
I know this seems extremely simple but I am just learning to code and i have been looking all over for an explanation why this isn't working yet I see it work on another pc exactly as is. Anyway any explanation would be greatly appreciated.
Here is what i found in command line If this answers your questions about what was in the cmd.
g++ -Wall -fexceptions -g -std=c++11 -Wall -I -c /home/gator/Documents/Spartan1/Gladius.cpp -o obj/Debug/Gladius.o
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start':
(.text+0x20): undefined reference tomain'
collect2: error: ld returned 1 exit status
Know the compiler options(gcc/g++ compiler):
-c : Compile and assemble, but do not link
-o file : Place the output into file
So when you run
g++ filename.cpp -o executable_name
, you generate an application which can be executed.
The problem is you are compiling, assembling as well as linking when you are trying to compile "Gladius.cpp" and compiler is trying to search for main() definition.
So in your case, the compilation steps would be:
First compile "Gladius.cpp" and generate object file "Gladius.o":
g++ -Wall -fexceptions -g -std=c++11 -c Gladius.cpp
Next compile "main.cpp" and generate object file "main.o":
g++ -Wall -fexceptions -g -std=c++11 -c main.cpp
Generate executable by linking "main.o" and "Gladius.o"
g++ -Wall -fexceptions -g -std=c++11 -o main main.o Gladius.o
Now you can run "main":
./main
Your compiler's command line contains -I -c sequence.
This -I option "swallows" your -c option. -I requires an additional argument, which is an include directory name. You failed to supply that argument, which is why -I assumes that -c that follows it is the directory name. So that -I consumes that -c.
The compiler never sees that -c. Without -c it assumes that you want to compile and link your program. Since Gladius.cpp does not have main in it, you get the error at linking stage.
Here 's a simple demo of the same problem: http://coliru.stacked-crooked.com/a/8a37cd3e90a443e2
You need to figure out why you have an orphaned -I in your command line.
If you are compiling this code using a command line like:
g++ -Wall -Wextra -Werror -O gladius.cpp -o output.exe
then make sure that you include all the .cpp files (not .h files) that contain code that your program needs.
g++ -Wall -Wextra -Werror -O gladius.cpp main.cpp -o output.exe
I explain this to beginners all the time as each .cpp being a bag of Lego's in a kit. You need all the bags that came with the box in order to build the kit. If you omitted main.cpp (or the file that contains main) then you will get the linker error that you are currently getting.
What command are you using to compile, link, and then execute? It should look something like
$ g++ main.cpp gladius.cpp -odemo
$ ./demo
check your command line for linking step.. You may forgot file with main as input, or you had forgot output file name after -o (and masked main.o in result)
I had this very kind of problem myself, and though it may not be the conventional, "proper" solution, I simply renamed the ".c" file to ".cpp", and it all worked.
After all, I was compiling both c and c++ together with a c++ compiler (recommended by the library), and the c code already had the proper c++ #extern flags (see here for more on that).
Also related:
C++ Error: undefined reference to `main'
Including C Code in C++
Why do you need an explicit `-lm` compiler option
Compilation on Linux - In function '_start': (.text+0x20): undefined reference to 'main'

g++ undefined reference to library symbols [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 5 years ago.
There are linker errors to Symbols defined by SFML, but i cannot see how they occur despite that I linked the lib.
I'm using make, which I currently learn and I want to build a minimalistic dev-environment with it.
Give a holler if you need anymore information than the following. I'd just like to minimize the questions size.
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make clean
rm -f build/*.o build/release/*.o build/debug/*.o build/test/*.o
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make tests
//test obj first
g++ -std=c++14 -Wall -pthread -Iinclude -c test/Packager.ut.cpp -o build/test/Packager.ut.o -g3
//now the src obj
g++ -std=c++14 -Wall -pthread -Iinclude -c src/ClientAddress.cpp -o build/debug/ClientAddress.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Packager.cpp -o build/debug/Packager.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Package.cpp -o build/debug/Package.o -g3
Built debug object files.
//now the first test itself
g++ -std=c++14 -Wall -pthread -Iinclude -lsfml-network build/test/Packager.ut.o build/debug/ClientAddress.o build/debug/Packager.o build/debug/Package.o -g3 -o bin/test/Packager.ut
build/test/Packager.ut.o: In function `main':
/home/XXX/Documents/dev/cpp/proj/beep/test/Packager.ut.cpp:69: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::ClientAddress::ClientAddress()':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:21: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::operator==(nw::udp::ClientAddress const&, nw::udp::ClientAddress const&)':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:33: undefined reference to `sf::operator==(sf::IpAddress const&, sf::IpAddress const&)'
...
and so on ... every mentionings of sf:: inside the files are quoted
I get the same error pattern if I try to compile the other tests (for ClientAddress for example)
Of course i now want to know what i linked wrong how there. As you can see the lib is linked with -lsfml-network. I also checked the SMFL installation, so it is at least less likely a lib file gone missing from standard directory.
I guess there is an error in my usage of g++ , compiling and linking orders or smth.
My project tree:
>bin
----mainexec
--->test
----.ut
>build
--->debug
----.o
--->release
----.o
--->test
----.ut.o
>src
---- .cpp
>include
---- .h
>test
---- .ut.cpp
As a second part of the question, I'd like to ask if there is a better way to build tests, because I simply link every src-obj with my test-obj, even if there are way more src-obj linked than it actually needs. It should work and I do not have to maintain my test dependencies all the time, which later would be very cumbersome. What is common ?
sfml-network has a dependency on sfml-system. Try add -lsfml-system before -lsfml-network in your linker command in your makefile

Undefined reference to 'dlsym' and 'dlopen'

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.

C++ / mysql Connector - undefined reference to get_driver_instance - already tried the easy stuff

Yes this question has been asked before ... I've tried everything mentioned in the previous answers. My setup is really straightforward so this shouldn't be so hard.
I just want to program against mysql using C++. My source code is taken verbatem from the 'hello world' type example here:
http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html
I am on Ubuntu 12.10. I am trying:
g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18 -L/usr/lib/mysqlcppconn -lmysqlcppconn firsttry.cpp
It compiles (if I use -c option) but won't build, giving me the infamous:
/tmp/ccn768hj.o: In function `main':
firsttry.cpp:(.text+0x3a): undefined reference to `get_driver_instance'
A few details:
'firsttry.cpp' is just what I named the source code file, again taken verbatem from the official example
As you can see I AM linking in the mysqlclient library and the mysqlcppconn library. Many times when this question has been asked previously, the answer was to link those.
Some other historical answers suggest the sample source code is wrong and that the function in question needs to be in the sql::mysql namespace etc. I am pretty sure the source code is fine. Again, it compiles, and changing the namespaces in the source code just seems to make it worse.
Thank you in advance for any help you can provide.
So I have now had this problem for a week now and I became very frustrated with it as well. I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. Here is what I have and I hope it helps.
I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with:
sudo apt-get install libmysqlcppconn-dev
And here is my simple tester source file "tester.cpp"
#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
using namespace sql;
int main(void){
sql::Driver *driver;
sql::Connection *con;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");
return 0;
}
And finally g++ compile command:
g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn
This worked for me and I hope it helps you solve your problem!
For me simply swapping the order of the last two arguments fixed this problem. I don't know why but the linker is able to find the function get_driver_instance if I specify the -lmysqlcppconn option at the end after the source file.
g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -L/usr/lib/mysqlcppconn firsttry.cpp -lmysqlcppconn
Also note that I took out the following options as I think they are redundant
-I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18
In case you are as forgetful as me and didn't link the library in CMakeLists.txt:
target_link_libraries(<target> mysqlcppconn)
If all the paths are included throw param -I. You would see whether there is a problem if you compile like this:
g++ -g -o0 -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g -o0 -L/usr/local/lib -L/usr/local/mysql/lib -lmysqlcppconn main.o -o test
the problem will appear:
main.o: In function `main':
/home/huangxw/workspace/public/soal/test/main.cpp:165: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status
Now you must adjust the order of -lmysqlcppconn and main.o:
g++ -g -o0 -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g -o0 -L/usr/local/lib -L/usr/local/mysql/lib main.o -o test -lmysqlcppconn
That is all!!
The reason is simple. You can find out using the web or ask me to elaborate.