Mac 端口 Mac 上的 OpenSceneGraph 链接器错误

OpenSceneGraph Linker error on Mac with Macports

我在 Mac 上将 OpenSceneGraph 项目正确设置为 link 时遇到问题。我通过 macports 安装它,我已经得到 osgviewer 和其他 osg 程序工作,这意味着它以某种方式被编译和 linked。该代码是一个更大项目的一部分,但是我将 OSG 代码分离到最基本的部分以隔离我的问题。我首先认为这是一个 CMake 问题,所以我创建了一个 Makefile 来尝试隔离那里。那没什么区别。

这是示例代码。

// OpenSceneGraph Libraries
#include <osg/Geode>
#include <osg/Group>
#include <osg/ShapeDrawable>
#include <osgUtil/Optimizer>
#include <osgUtil/SmoothingVisitor>
#include <osgUtil/Simplifier>
#include <osg/Node>
#include <osg/Texture1D>
#include <osg/Texture2D>
#include <osg/TexGen>
#include <osg/Material>
#include <osgViewer/Viewer>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osgDB/ReadFile>
#include <osgSim/Version>
#include <osgFX/Version>
#include <osgTerrain/Version>
#include <osgVolume/Version>



// C++ Libraries 
#include <iostream>
#include <string>


/**
 * @brief Main Application
*/
int main( int argc, char* argv[] )
{

    // Define Write Options
    osgDB::Options* write_options = new osgDB::Options("WriteImageHint=IncludeData Compressor=zlib");

    // Create the Root Nodes
    osg::ref_ptr<osg::Group> root_node(new osg::Group());



    // Write the Node File
    osgDB::writeNodeFile( *root_node.get(),
                           "output.osgb",
                           write_options );

    // Return
    return 0;
}

这是生成文件代码。我添加了超出需要的方式,但没有效果。

OSG_LIBS=-lOpenThreads -losgDB -losg -losgUtil -losgTerrain

LIBS=-L/opt/local/lib $(OSG_LIBS)
INCL=-I/opt/local/include

CPP=clang++

foo: foo.cpp
    $(CPP) foo.cpp $(LIBS) $(INCL)

这是特定命令的 VERBOSE=1 的 make 输出。

$ make VERBOSE=1
clang++ foo.cpp -L/opt/local/lib -lOpenThreads -losgDB -losg -losgUtil -losgTerrain -I/opt/local/include
Undefined symbols for architecture x86_64:
  "osgDB::writeNodeFile(osg::Node const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, osgDB::Options const*)", referenced from:
  _main in foo-2c45f6.o
  "osgDB::Options::parsePluginStringData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char, char)", referenced from:
  osgDB::Options::Options(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in foo-2c45f6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [foo] Error 1

我有 运行 otool 和其他针对共享库的应用程序,试图找到未定义引用的位置。我知道当我 运行 nm 时,这两种方法的符号都在 libosgDB.dylib 中,但是 link 它不起作用。

我开始从源代码编译 OpenSceneGraph,但这也会自行产生编译错误。我前面还有一场漫长的战斗。非常感谢您抽出时间。

更新:

观察结果

嗯,我可以重现你的问题。似乎 libosgDB.dylib 由 MacPorts 的 OpenSceneGraph 端口 links 针对 libstdc++ 安装:

$ otool -L /opt/local/lib/libosgDB.dylib
/opt/local/lib/libosgDB.dylib:
    /opt/local/lib/libosgDB.100.dylib (compatibility version 100.0.0, current version 3.2.1)
    /opt/local/lib/libosgUtil.100.dylib (compatibility version 100.0.0, current version 3.2.1)
    /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 157.0.0)
    /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
    /opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8)
    /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
    /opt/local/lib/libosg.100.dylib (compatibility version 100.0.0, current version 3.2.1)
    /opt/local/lib/libOpenThreads.20.dylib (compatibility version 20.0.0, current version 3.2.1)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.1.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1255.1.0)
    /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 728.4.0)

我认为这是 OpenSceneGraph 端口的错误。请提交工单以解决此问题。同时,在您的编译器命令行中使用 -stdlib=libstdc++ 将您的示例设为 link。请注意,这意味着您不能使用 C++11 功能。