2

Gecode(4.3.0)文档规定,在Mac上安装Gecode后,可以编译链接示例如下:

g++ -O3 -c money.cpp
g++ -framework gecode -o money money.o

编译成功但链接失败:

Undefined symbols for architecture x86_64:
  "Gecode::Gist::TextOutput::TextOutput(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      void Gecode::Driver::ScriptBase<Gecode::Space>::runMeta<Money, Gecode::DFS, Gecode::Options, Gecode::Driver::EngineToMeta>(Gecode::Options const&, Money*) in money.o
      void Gecode::Driver::ScriptBase<Gecode::Space>::runMeta<Money, Gecode::DFS, Gecode::Options, Gecode::RBS>(Gecode::Options const&, Money*) in money.o
  "Gecode::Driver::stop(Gecode::Support::Timer&, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
      void Gecode::Driver::ScriptBase<Gecode::Space>::runMeta<Money, Gecode::DFS, Gecode::Options, Gecode::Driver::EngineToMeta>(Gecode::Options const&, Money*) in money.o
      void Gecode::Driver::ScriptBase<Gecode::Space>::runMeta<Money, Gecode::DFS, Gecode::Options, Gecode::RBS>(Gecode::Options const&, Money*) in money.o
  "Gecode::branch(Gecode::Home, Gecode::IntVarArgs const&, Gecode::IntVarBranch, Gecode::IntValBranch, bool (*)(Gecode::Space const&, Gecode::IntVar, int), void (*)(Gecode::Space const&, Gecode::BrancherHandle const&, unsigned int, Gecode::IntVar, int, int const&, std::__1::basic_ostream<char, std::__1::char_traits<char> >&))", referenced from:
      Money::Money(Gecode::Options const&) in money.o
ld: symbol(s) not found for architecture x86_64

知道如何解决这个问题吗?

4

2 回答 2

3

我遇到过同样的问题。有趣的是,Gecode 中示例文件夹中的所有源文件都在编译 Gecode 期间成功编译和链接。在尝试了各种包含路径、库路径和库名称之后,我放弃了并做了一些研究。

似乎问题源于编译 Gecode 本身。如果您使用默认的 Xcode 设置编译/链接,即您使用调用 clang (Apple LLVM 6.0) 的 gcc (4.2.1) 符号链接,您应该确保 Gecode 和您的程序都使用相同的标准库。那是因为有旧的二进制文件(最初与本机 gcc 一起使用)和更新的二进制文件。

我使用 gcc 4.9.2(使用 MacPorts)编译了 Gecode。出于某种原因,我切换回 gcc 4.2.1/clang。为了编译我的 Gecode 程序,我必须添加-stdlib=libstdc++编译/链接指令。这链接到较旧的二进制文件,而stdlib=libc++链接到较新的二进制文件。编译 Send-More-Money 将如下所示:

g++ -c -stdlib=libstdc++ -I/usr/local/include money.cpp
g++ -stdlib=libstdc++ -L/usr/local/lib money.o -lgecodedriver -lgecodesearch -lgecodeminimodel -lgecodeint -lgecodekernel -lgecodesupport

另一方面,使用 gcc 4.9.2 编译 Gecode 程序很简单。事实上,较新版本的 g++ 甚至不接受 option -stdlib。因此它只是

g++ -c -I/usr/local/include money.cpp
g++ -L/usr/local/lib money.o -lgecodedriver -lgecodesearch -lgecodeminimodel -lgecodeint -lgecodekernel -lgecodesupport

这里的所有都是它的。归功于Marco Correia(参见gecode 邮件列表)。

于 2014-11-21T16:42:12.720 回答
0

您可以使用文档中的 Linux 命令来链接 dylib,例如:

  1. setevn LD_LIBRARY_PATH <dir,例如:/usr/local/>

  2. g++ -I <dir>/include -c send-more-money.cpp

  3. g++ -o send-more-money -L<dir>/lib send-more-money.o -lgecodesearch -lgecodeint -lgecodekernel -lgecodesupport

于 2014-10-21T05:31:40.320 回答