我是做Java开发的,有一个项目需要三维视景,初步设想是用OSG来做视景,发布成DLL,然后Java用JNA方式调用。我想得到两个方面的帮助:
1、用OSG开发的视景到底能不能发布成DLL;
2、如果能发布成DLL的话,我遇到的编译问题怎么解决。
由于我不太了解C++,所以,只能是艰难的进行试验,这两天我完成了以下步骤:
1、编译了OSG 3.0.0,通过CMD运行“osgViewer cow.osg”能够显示;
2、在VS2010中建立一个Win32控制台应用,能够正常运行(在这一步里面我了解了包含目录、库目录、依赖项目的相关问题)。代码如下#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
int main( int argc, char **argv ){
osgViewer::Viewer viewer; 
osg::Node *node = new osg::Node;
node = osgDB::readNodeFile("glider.osg");
viewer.setSceneData(node); 
viewer.setUpViewInWindow(200, 200, 500, 500, 0); viewer.realize(); 
return viewer.run(); 
}3、建立了一个DLL工程,编译出的DLL提供的接口函数,能够在Java中通过JNA方式调用。
4、在上述工程中,添加如下接口函数(此函数的代码与Win32工程相同,包含目录、库目录、依赖项目设置与Win32工程相同),编译不能通过。__declspec(dllexport) int showCow(int x, int y, int w,int h){
osgViewer::Viewer viewer; 
osg::Node *node = new osg::Node;
node = osgDB::readNodeFile("glider.osg");
    viewer.setSceneData(node); 
viewer.setUpViewInWindow(x, y, w, h, 0); viewer.realize(); 
    return viewer.run(); 
}编译错误提示如下:1>     正在创建库 E:\OSG_LX\OSG_test\x64\Debug\Hello.lib 和对象 E:\OSG_LX\OSG_test\x64\Debug\Hello.exp
1>Hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl osgViewer::Viewer::`vbase destructor'(void)" (__imp_??_DViewer@osgViewer@@QEAAXXZ),该符号在函数 "int __cdecl showCow(int,int,int,int)" (?showCow@@YAHHHHH@Z) 中被引用
1>Hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual int __cdecl osgViewer::Viewer::run(void)" (__imp_?run@Viewer@osgViewer@@UEAAHXZ),该符号在函数 "int __cdecl showCow(int,int,int,int)" (?showCow@@YAHHHHH@Z) 中被引用与上述提示类似的共38个。

解决方案 »

  1.   

    编译不通过 应该是需要添加 对应的 lib 库
    能不能发布, 要看许可权益等协议, 一般的非商业用途的没有问题
      

  2.   

    包含目录、库目录、依赖项这几个问题我应该是没有做错,因为与win32项目时一样的,而Win32项目是可以运行的。
      

  3.   

    C 和 C++ 默认的调用方式不一样,  试试 extern "C"#ifdef __cplusplus
    extern "C" {
    #endif#include <osgViewer/Viewer>
    #include <osgDB/ReadFile>#ifdef __cplusplus
    }
    #endif
      

  4.   

    我对C/C++的了解太可怜了,我把代码改成如下这样,大神帮着看看,是该这样做吗?
    Hello.h的内容:#ifdef __cplusplus
    extern "C" {
    #endif#include <stdio.h>
    #include <Windows.h>#include <osgViewer/Viewer>
    #include <osgDB/ReadFile>
    #ifndef _HELLO_H_
    #define _HELLO_H___declspec(dllexport) void print_hello();
    __declspec(dllexport) int add(int a, int b);
    __declspec(dllexport) int showCow(int x, int y, int w,int h);#endif#ifdef __cplusplus
    }
    #endifHello.cpp的内容:#include "Hello.h"__declspec(dllexport) void print_hello()
    {
    printf("Hello, world!\n");
    return;
    }__declspec(dllexport) int add(int a, int b){
    return a+b;
    }__declspec(dllexport) int showCow(int x, int y, int w,int h){
    osgViewer::Viewer viewer; 
    osg::Node *node = new osg::Node;
    node = osgDB::readNodeFile("glider.osg");
        viewer.setSceneData(node); 
    viewer.setUpViewInWindow(x, y, w, h, 0); viewer.realize(); 
        return viewer.run(); 
    }以下是编译时的出错信息,这些出错信息从结构上看有3中,我每种选出了一条,请大神帮忙看看,问题出在哪里。1>E:\OSG\include\osg/ref_ptr(21): error C2894: 模板不能声明为有“C”链接1>E:\OSG\include\osg/ref_ptr(128): warning C4190: “static_pointer_cast”有指定的 C 链接,但返回了与 C 不兼容的 UDT“osg::ref_ptr<T>”1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception(449): error C2526: “std::_Exception_ptr::_Current_exception”: C 链接函数无法返回 C++ 类“std::_Exception_ptr”
    1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception(413) : 参见“std::_Exception_ptr”的声明
      

  5.   

    参考下  OSG3.4.0+VS2010+WIN10编译及二次开发环境搭建
      

  6.   

    大神,我做的Win32应用可以运行,不能证明OSG编译正确吗?我还需要重新编译吗?我遇到的问题还可能有什么其他原因吗?请大神再帮忙想想,不太懂C++,初次接触OSG,实在是一点思路没有,给你添麻烦了。