Home > Net >  C Linking error using libraries libsumo, libtraci, undefined reference to libtraci::Simulation::in
C Linking error using libraries libsumo, libtraci, undefined reference to libtraci::Simulation::in

Time:05-19

I have a question regarding correctly linking libraries libsumocpp and libtracicpp in Windows.

I'm trying to connect to a sumo server using libsumo in C and have the following code:

#include <iostream>
#include <libsumo/libtraci.h>

using namespace libtraci;

int main(int argc, char* argv[]) {

Simulation::init();
//Simulation::start({"sumo", "-c", "Network_02.sumocfg"});
Simulation::setOrder(2);
//Simulation::init();
//Simulation::setOrder(2);

for (int i=0; i<50; i  )
{
    Simulation::step();
}
Simulation::close();
return 0;
}

The code above is based on the following C code snippet (this github post) which tries to connect to a sumo server:

I'm trying to connect to the Sumo simulation from multiple traci clients using libtraci. 
This is how I'm starting the simulation:

sumo --remote-port 4001 --num-clients 2 -c config_file.sumocfg

After starting the simulation I'm trying to connect from two traci client using   the 
   code below:

#include <libsumo/libtraci.h>

using namespace libtraci;
int main () {
Simulation::init(4001,21,"localhost");
Simulation::setOrder(3);
for (int i = 0; i < 500000; i  ) {
Simulation::step();
}
Simulation::close();
return 0;
}

As already mentioned, my code is the one adapted above. I get the following errors when builiding, compiling and linking as the class "Simulation" functions listed under "libsumo/Simulation.h" seem not to be linked correctly:

c:/mingw64/bin/../ld.exe: src\Test.o:C:\Users\Lukas\eclipse-ws\Test\Debug/../src/Test.cpp:28: 
undefined reference to `libtraci::Simulation::init(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, _iobuf*)'
c:/mingw64/bin/../bin/ld.exe: src\Test.o: in function `main':
C:\Users\Lukas\eclipse-ws\Test\Debug/../src/Test.cpp:30: undefined reference to `libtraci::Simulation::setOrder(int)'
c:/mingw64/bin/../ld.exe: C:\...\Test.cpp:36: undefined reference to `libtraci::Simulation::step(double)'
c:/mingw64/bin/../ C:.../src/Test.cpp:38: undefined reference to `libtraci::Simulation::close(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2.exe: error: ld returned 1 exit status

I guess it is a linking error. I have provided the path (-L "C:\Users\Lukas\eclipse\SUMO\bin") to the libraries "libsumocpp.lib/dll" and "libtracicpp.lib/dll" (-l sumocpp -ltracicpp) in Properties -> C/C Build -> Settings -> MinGW C Linker. However, it does not work. Does anyone have a clue what might be wrong in my setup?

Best regards, Lukas

CodePudding user response:

gcc/binutils uses extension .a (or even .dll.a for shared libraries) instead of .lib (used by MSVC) as library import files.

So to link you should have lib<name>.a (static) or lib<name>.dll.a (shared) files in a folder pointed to with -L and then use -l<name> to link with the library.

gcc (not sure about ld) is smart enough to know the .dll itself is a shared library, so you can even specify the the path of a .dll file and it will know you want to link against that shared library.

CodePudding user response:

undefined reference to libtraci::Simulation::init(int, int, std::__cxx11::basic_string<...

The error suggests that you are compiling your code with -std=c 11 flag.

But libtraci may not be built with that flag, and if it's not, then you would get exactly the errors you are getting.

Run nm -C libtracicpp.lib | grep 'libtraci::Simulation::init' and see whether anything shows up, and whether the demangled symbol has ::__cxx11:: in it.

If there is no __cxx11 in the demangled name, you'll need to either rebuild libtracicpp with -std=c 11, or build your code without that flag (your sample doesn't appear to use any C 11 features).

  • Related