C++使用库libtraci链接错误,对libtraci :: simulation :: init()的未定义引用
我有一个问题,请问正确链接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 ++链接器。 但是,它行不通。有人知道我的设置可能出了什么问题吗?
此致, 卢卡斯
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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) orlib<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.错误表明您将代码编译给
-STD = c c, flag
dime nibtraci :: simulation :: init(int,int,std :: __ cxx11 :: basic_string&lt; ...
++ 11nm -c libtracicpp.lib | grep'libtraci :: simulation :: init'
,看看是否出现任何内容 。它 或在没有该标志的情况下构建代码(您的示例似乎没有使用任何
c ++ 11
功能)。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 rebuildlibtracicpp
with-std=c++11
, or build your code without that flag (your sample doesn't appear to use anyC++11
features).