因为osgDB有自定义的ifstream和ofstream,和std标准库,所以我的一个程序报了错
错误 LNK2005 "public: __cdecl std::basic_ifstream<char,struct std::char_traits<char> >::basic_ifstream<char,struct std::char_traits<char> >(void)" (??0?$basic_ifstream@DU?$char_traits@D@std@@@std@@QEAA@XZ) 已经在 Configuration.cpp.obj 中定义 D:\osg\osgRotateTest D:\osg\osgDBd.lib(osg130-osgDBd.dll)
因为我用cmake管理vc的工程,不好在链接命令行加/FORCE:MULTIPLE ,我看了看怎样在cmakelists文件里设置链接选项,
target_link_options(${PROJNAME} PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/FORCE:MULTIPLE>)
把这个加入CMakelists文件,重新编译,没有报错了,这是windows下Visual studio 2019里的做法,我还得在linux下试试。有的人的做法不一样,是在cmake配置时,把每个模块目录路径都设置为osg的root目录,我先记录一下,再做做实验研究一下人家的做法。
https://stackoverflow.com/questions/58008297/how-to-add-compile-flags-in-cmake-on-windows
另外附上我的cmake文件
####
cmake_minimum_required( VERSION 2.8 )
project(osgRotateTest)
find_package(OpenSceneGraph REQUIRED COMPONENTS OpenThreads osg osgWidget osgText osgSim osgDB osgGA osgUtil osgViewer osgManipulator)
INCLUDE_DIRECTORIES( include
${OPENSCENEGRAPH_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/inclde
${PROJECT_SOURCE_DIR}/application
)
LINK_DIRECTORIES(${OPENSCENEGRAPH_LIB_DIR})
file(GLOB srcs "*.h" "*.cpp"
"pong/*.cpp"
"Project/*.cpp"
"helicopter/*.cpp"
"helicopter/tests/*.cpp"
)
macro( config_project PROJNAME LIBNAME )
include_directories( ${${LIBNAME}_INCLUDE_DIR} )
target_link_options(${PROJNAME} PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/FORCE:MULTIPLE>)
target_link_libraries( ${PROJNAME} ${${LIBNAME}_LIBRARIES} ${OPENSCENEGRAPH_LIBRARIES})
endmacro()
add_executable( MyProject ${srcs})
config_project( MyProject OPENTHREADS )
config_project( MyProject OSG )
config_project( MyProject OSGGA )
config_project( MyProject OSGTEXT )
config_project( MyProject OSGDB )
config_project( MyProject OSGUTIL )
config_project( MyProject OSGVIEWER )
config_project( MyProject OSGMANIPULATOR )