如何正确地链接和包含库



这是我第一次用c++ (Visual Studio 2010)准备一个相当复杂的应用程序,我有严重的链接问题,这让我很头疼(可能是因为我从来没有得到它的全部内容)。

我正在实现流体流动模拟器使用:NVidia PhysX (SDK 3.2.3)-辉光(1.9)- freeglut (2.8.1)- gltools库(OpenGL Superbible 5th插件)

我不知道如何正确设置Release/Debug和MD/MDd/MT/MTd选项以使一切正常工作。

  • 我可以用MD/MDd/MT/MTd编译freeglut,也可以用release/Release-Static/Debug/Debug- static解决方案配置
  • 我可以用MD/MDd/MT/MTd和release/Debug编译gltools
  • PhysX是预编译的库和dll文件(我不知道在编译过程中使用了什么选项)

我尝试了很多配置,但我总是得到编译错误,如

defaultlib 'MSVCRT' conflicts with use of other libs;

fatal error C1189: #error :  Exactly one of NDEBUG and _DEBUG needs to be defined by preprocessor

我读到这都是关于以正确的方式设置提到的配置,但我实际上做不到。

什么应该是正确的配置(为什么?)和哪些库我应该与lib文件链接,哪些与dll?我对c++算是新手,所以请尽量解释清楚。

这是我的应用程序的"包含部分"(它非常相似,在我不小心破坏它之前它工作):

//#define WIN32
#include <windows.h>        // Must have for Windows platform builds
#include <GLTools.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLMatrixStack.h>
#include <GLShaderManager.h>
#include <GLGeometryTransform.h>
#include <StopWatch.h>
//#define GLEW_STATIC
#include <GLglew.h>            // OpenGL Extension "autoloader"
//#define FREEGLUT_STATIC
#include <GLfreeglut.h>
//#include <GLgl.h>            // Microsoft OpenGL headers (version 1.1 by themselves)
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include <PxPhysicsAPI.h> 
#include <extensions/PxExtensionsAPI.h> 
#include <extensions/PxDefaultErrorCallback.h>
#include <extensions/PxDefaultAllocator.h> 
#include <extensions/PxDefaultSimulationFilterShader.h>
#include <extensions/PxDefaultCpuDispatcher.h>
#include <extensions/PxShapeExt.h>
#include <foundation/PxMat33.h> 
#include <pxtask/PxCudaContextManager.h>
#include <physxprofilesdkPxProfileZoneManager.h>
#include <extensions/PxSimpleFactory.h>
#pragma comment(lib, "PhysX3_x86.lib")
#pragma comment(lib, "PhysX3Common_x86.lib") 
#pragma comment(lib, "PxTask.lib")
#pragma comment(lib, "PhysX3Extensions.lib")
#pragma comment(lib, "PhysXProfileSDK")

我不确定如果一切都设置得很好,最后的车道是否必要(在谷歌的某个地方发现它,它工作)。

如果您需要更多的信息或更详细的配置错误日志,请告诉我,我将提供。

提前感谢!

编辑:

调试/MDd

PxTask.lib(CudaWrapper.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultSimulationFilterShader.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtSimpleFactory.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultCpuDispatcher.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultErrorCallback.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtRigidBodyExt.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtCpuWorkerThread.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysXProfileSDK.lib(PxProfileEventImpl.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2019: unresolved external symbol _glewInit@0 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>C:devSPHFluidsSPHFluidsDebugSPHFluids.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

发布/MDd

PxTask.lib(CudaWrapper.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultSimulationFilterShader.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultErrorCallback.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultCpuDispatcher.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtSimpleFactory.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtCpuWorkerThread.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtRigidBodyExt.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysXProfileSDK.lib(PxProfileEventImpl.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2001: unresolved external symbol _glewInit@0
1>C:devSPHFluidsSPHFluidsReleaseSPHFluids.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

发布/MTd

PxTask.lib(CudaWrapper.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultSimulationFilterShader.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultErrorCallback.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtDefaultCpuDispatcher.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtSimpleFactory.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtCpuWorkerThread.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysX3Extensions.lib(ExtRigidBodyExt.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>PhysXProfileSDK.lib(PxProfileEventImpl.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: _fclose already defined in LIBCMTD.lib(fclose.obj)
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: _fwrite already defined in LIBCMTD.lib(fwrite.obj)
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: _free already defined in LIBCMTD.lib(dbgfree.obj)
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: _malloc already defined in LIBCMTD.lib(dbgmalloc.obj)
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: _fgetc already defined in LIBCMTD.lib(fgetc.obj)
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: _fprintf already defined in LIBCMTD.lib(fprintf.obj)
1>MSVCRT.lib(MSVCR100.dll) : error LNK2005: ___iob_func already defined in LIBCMTD.lib(_file.obj)
1>MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)
1>MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>libcpmtd.lib(xdebug.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2001: unresolved external symbol _glewInit@0
1>C:devSPHFluidsSPHFluidsReleaseSPHFluids.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

不含d (/MD/MT):

c:devsoftphysx-3.2.3_pc_sdk_coreincludefoundationpxpreprocessor.h(318): fatal error C1189: #error :  Exactly one of NDEBUG and _DEBUG needs to be defined by preprocessor

,它只是lib文件的一个配置。我可以编译gltools和freeglut的其他方式或设置#define GLEW_STATIC或#define freeglu_static其他方式…对新手来说选择太多了。:)

一个:

1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2001: unresolved external symbol "class physx::PxFlags<enum physx::PxFilterFlag::Enum,unsigned short> __cdecl physx::PxDefaultSimulationFilterShader(unsigned int,struct physx::PxFilterData,unsigned int,struct physx::PxFilterData,class physx::PxFlags<enum physx::PxPairFlag::Enum,unsigned short> &,void const *,unsigned int)" (?PxDefaultSimulationFilterShader@physx@@YA?AV?$PxFlags@W4Enum@PxFilterFlag@physx@@G@1@IUPxFilterData@1@I0AAV?$PxFlags@W4Enum@PxPairFlag@physx@@G@1@PBXI@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall physx::PxDefaultErrorCallback::~PxDefaultErrorCallback(void)" (??1PxDefaultErrorCallback@physx@@UAE@XZ)
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall physx::PxDefaultErrorCallback::PxDefaultErrorCallback(void)" (??0PxDefaultErrorCallback@physx@@QAE@XZ)
1>main.obj : error LNK2001: unresolved external symbol "public: static class physx::PxProfileZoneManager & __cdecl physx::PxProfileZoneManager::createProfileZoneManager(class physx::PxFoundation *)" (?createProfileZoneManager@PxProfileZoneManager@physx@@SAAAV12@PAVPxFoundation@2@@Z)
1>main.obj : error LNK2001: unresolved external symbol "class physx::pxtask::CudaContextManager * __cdecl physx::pxtask::createCudaContextManager(class physx::PxFoundation &,class physx::pxtask::CudaContextManagerDesc const &,class physx::PxProfileZoneManager *)" (?createCudaContextManager@pxtask@physx@@YAPAVCudaContextManager@12@AAVPxFoundation@2@ABVCudaContextManagerDesc@12@PAVPxProfileZoneManager@2@@Z)
1>main.obj : error LNK2001: unresolved external symbol "class physx::PxDefaultCpuDispatcher * __cdecl physx::PxDefaultCpuDispatcherCreate(unsigned int,unsigned int *)" (?PxDefaultCpuDispatcherCreate@physx@@YAPAVPxDefaultCpuDispatcher@1@IPAI@Z)
1>main.obj : error LNK2001: unresolved external symbol __imp__PxCreateFoundation
1>main.obj : error LNK2001: unresolved external symbol _glewInit@0
1>main.obj : error LNK2001: unresolved external symbol _PxCreatePlane
1>main.obj : error LNK2001: unresolved external symbol __imp__PxRegisterArticulations
1>main.obj : error LNK2001: unresolved external symbol __imp__PxRegisterHeightFields
1>main.obj : error LNK2001: unresolved external symbol __imp__PxCreateBasePhysics
1>C:devSPHFluidsSPHFluidsReleaseSPHFluids.exe : fatal error LNK1120: 12 unresolved externals
1>
1>Build FAILED.

这是所有这些PhysX pragmas注释。

有一件事可能会给您指出解决方案,那就是physx 3.2.3 lib文件是用/MT选项构建的,正如它们的文档中所指出的那样(这是文档文件夹中physx zip的一部分):

"我们在Windows二进制发行版中提供的静态库与多线程静态C运行时(CRT)库相关联。这意味着您的应用程序也必须使用相同的CRT样式。如果您需要使用不同的CRT版本,您必须升级到我们的源许可证。可以使用不同的CRT设置简单地重新编译源代码分布。"因此,如果您计划对这些库进行链接,则必须对所有其他库也使用/MT标志。

在包含物理头文件的文件中添加#define NDEBUG

最新更新