C++使用库libtraci链接错误,对libtraci :: simulation :: init()的未定义引用

发布于 2025-01-30 05:24:25 字数 2513 浏览 2 评论 0 原文

我有一个问题,请问正确链接windows中的libsumocpp和libtracicpp。

我正在尝试使用C ++中的libsumo连接到Sumo服务器,并具有以下代码:

#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;
}

上面的代码基于以下C ++代码片段(此github帖子)试图连接到Sumo服务器的:

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

如上所述,我的代码是上面改编的一个。 在“ libsumo/simulation.h”下列出的类“仿真”函数时,我会在构建,编译和链接时会遇到以下错误,似乎无法正确链接:

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

我猜这是一个链接错误。我已将路径(-l“ C:\ users \ lukas \ eclipse \ eclipse \ sumo \ bin”提供给库中的“ libsumocpp.lib/dll”和“ libtracicppp.lib/dll”(-l sumocpp-ltracicpp) - &gt; C/C ++构建 - &GT;设置 - &GT; Mingw C ++链接器。 但是,它行不通。有人知道我的设置可能出了什么问题吗?

此致, 卢卡斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短叹 2025-02-06 05:24:25

GCC/Binutils使用扩展 .a (甚至 .dll.a 共享库)而不是 .lib (由MSVC使用)为库导入文件。

因此,要链接您应该具有 lib&lt; name&gt; .a (static)或 lib&lt; name&gt; .dll.a (共享)文件夹中的文件夹指向 -l <​​/code>,然后使用 -L&lt; name&gt; 与库链接。

GCC(不确定LD)足够聪明,可以知道 .dll 本身是共享库,因此您甚至可以指定 .dll 文件的路径,并且将知道您想链接到该共享库。

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.

隐诗 2025-02-06 05:24:25

对libtraci :: simulation :: init(int,int,std :: __ cxx11 :: basic_string&lt; ...

错误表明您将代码编译给 -STD = c c, flag

dime nibtraci :: simulation :: init(int,int,std :: __ cxx11 :: basic_string&lt; ... ++ 11 nm -c libtracicpp.lib | grep'libtraci :: simulation :: init',看看是否出现任何内容 。

它 或在没有该标志的情况下构建代码(您的示例似乎没有使用任何 c ++ 11 功能)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文