我是做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、用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个。
能不能发布, 要看许可权益等协议, 一般的非商业用途的没有问题
extern "C" {
#endif#include <osgViewer/Viewer>
#include <osgDB/ReadFile>#ifdef __cplusplus
}
#endif
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”的声明