我正在尝试在我的游戏引擎中实现PhysX,但是在链接PhysX库时遇到了一些奇怪的问题。无论我做什么,它总是失败,但是Nvidia的片段却像个魅力。我将尝试描述自己的所作所为,希望有人能找到我所缺少的。
首先,我从Github下载了PhysX 4.1。然后我将buildtools设置更改为那些设置:
<?xml version="1.0" encoding="utf-8"?>
<preset name="vc15win64" comment="VC15 Win64 PhysX general settings">
<platform targetPlatform="win64" compiler="vc15" />
<CMakeSwitches>
<cmakeSwitch name="PX_BUILDSNIPPETS" value="True" comment="Generate the snippets" />
<cmakeSwitch name="PX_BUILDPUBLICSAMPLES" value="True" comment="Generate the samples projects" />
<cmakeSwitch name="PX_GENERATE_STATIC_LIBRARIES" value="True" comment="Generate static libraries" />
<cmakeSwitch name="NV_USE_STATIC_WINCRT" value="False" comment="Use the statically linked windows CRT" />
<cmakeSwitch name="NV_USE_DEBUG_WINCRT" value="True" comment="Use the debug version of the CRT" />
<cmakeSwitch name="PX_FLOAT_POINT_PRECISE_MATH" value="True" comment="Float point precise math" />
</CMakeSwitches>
<CMakeParams>
<cmakeParam name="CMAKE_INSTALL_PREFIX" value="install/vc15win64/PhysX" comment="Install path relative to PhysX SDK root" />
</CMakeParams>
</preset>
然后,我使用bat脚本为静态链接和运行时静态库生成Visual Studio 2017项目。然后,我以debug和release版本编译了该项目。 SnippestHelloWorld可以正常工作。到现在为止还挺好。因此,我在Visual Studio 2017中创建了一个新项目,然后选择了[[x64处理器体系结构(例如在PhysX xml配置中)。然后,以与SnippestHelloWorld中相同的方式包含头文件。这是一棵树:
| PxActor.h
| PxAggregate.h
| PxArticulation.h
| PxArticulationBase.h
| PxArticulationJoint.h
| PxArticulationJointReducedCoordinate.h
| PxArticulationLink.h
| PxArticulationReducedCoordinate.h
| PxBatchQuery.h
| PxBatchQueryDesc.h
| PxBroadPhase.h
| PxClient.h
| PxConfig.h
| PxConstraint.h
| PxConstraintDesc.h
| PxContact.h
| PxContactModifyCallback.h
| PxDeletionListener.h
| PxFiltering.h
| PxForceMode.h
| PxFoundation.h
| PxImmediateMode.h
| PxLockedData.h
| PxMaterial.h
| PxPhysics.h
| PxPhysicsAPI.h
| PxPhysicsSerialization.h
| PxPhysicsVersion.h
| PxPhysXConfig.h
| PxPruningStructure.h
| PxQueryFiltering.h
| PxQueryReport.h
| PxRigidActor.h
| PxRigidBody.h
| PxRigidDynamic.h
| PxRigidStatic.h
| PxScene.h
| PxSceneDesc.h
| PxSceneLock.h
| PxShape.h
| PxSimulationEventCallback.h
| PxSimulationStatistics.h
| PxVisualizationParameter.h
| stack.txt
|
+---characterkinematic
| PxBoxController.h
| PxCapsuleController.h
| PxController.h
| PxControllerBehavior.h
| PxControllerManager.h
| PxControllerObstacles.h
| PxExtended.h
|
+---collision
| PxCollisionDefs.h
|
+---common
| | PxBase.h
| | PxCollection.h
| | PxCoreUtilityTypes.h
| | PxMetaData.h
| | PxMetaDataFlags.h
| | PxPhysicsInsertionCallback.h
| | PxPhysXCommonConfig.h
| | PxProfileZone.h
| | PxRenderBuffer.h
| | PxSerialFramework.h
| | PxSerializer.h
| | PxStringTable.h
| | PxTolerancesScale.h
| | PxTypeInfo.h
| |
| \---windows
| PxWindowsDelayLoadHook.h
|
+---cooking
| PxBVH33MidphaseDesc.h
| PxBVH34MidphaseDesc.h
| PxBVHStructureDesc.h
| Pxc.h
| PxConvexMeshDesc.h
| PxCooking.h
| PxMidphaseDesc.h
| PxTriangleMeshDesc.h
|
+---cudamanager
| PxCudaContextManager.h
| PxCudaMemoryManager.h
|
+---extensions
| PxBinaryConverter.h
| PxBroadPhaseExt.h
| PxCollectionExt.h
| PxConstraintExt.h
| PxContactJoint.h
| PxConvexMeshExt.h
| PxD6Joint.h
| PxD6JointCreate.h
| PxDefaultAllocator.h
| PxDefaultCpuDispatcher.h
| PxDefaultErrorCallback.h
| PxDefaultSimulationFilterShader.h
| PxDefaultStreams.h
| PxDistanceJoint.h
| PxExtensionsAPI.h
| PxFixedJoint.h
| PxJoint.h
| PxJointLimit.h
| PxMassProperties.h
| PxPrismaticJoint.h
| PxRaycastCCD.h
| PxRepXSerializer.h
| PxRepXSimpleType.h
| PxRevoluteJoint.h
| PxRigidActorExt.h
| PxRigidBodyExt.h
| PxSceneQueryExt.h
| PxSerialization.h
| PxShapeExt.h
| PxSimpleFactory.h
| PxSmoothNormals.h
| PxSphericalJoint.h
| PxStringTableExt.h
| PxTriangleMeshExt.h
|
+---filebuf
| PxFileBuf.h
|
+---foundation
| | Px.h
| | PxAllocatorCallback.h
| | PxAssert.h
| | PxBitAndData.h
| | PxBounds3.h
| | PxErrorCallback.h
| | PxErrors.h
| | PxFlags.h
| | PxFoundationConfig.h
| | PxIntrinsics.h
| | PxIO.h
| | PxMat33.h
| | PxMat44.h
| | PxMath.h
| | PxMathUtils.h
| | PxMemory.h
| | PxPlane.h
| | PxPreprocessor.h
| | PxProfiler.h
| | PxQuat.h
| | PxSharedAssert.h
| | PxSimpleTypes.h
| | PxStrideIterator.h
| | PxTransform.h
| | PxUnionCast.h
| | PxVec2.h
| | PxVec3.h
| | PxVec4.h
| |
| +---unix
| | PxUnixIntrinsics.h
| |
| \---windows
| PxWindowsIntrinsics.h
|
+---geometry
| PxBoxGeometry.h
| PxBVHStructure.h
| PxCapsuleGeometry.h
| PxConvexMesh.h
| PxConvexMeshGeometry.h
| PxGeometry.h
| PxGeometryHelpers.h
| PxGeometryQuery.h
| PxHeightField.h
| PxHeightFieldDesc.h
| PxHeightFieldFlag.h
| PxHeightFieldGeometry.h
| PxHeightFieldSample.h
| PxMeshQuery.h
| PxMeshScale.h
| PxPlaneGeometry.h
| PxSimpleTriangleMesh.h
| PxSphereGeometry.h
| PxTriangle.h
| PxTriangleMesh.h
| PxTriangleMeshGeometry.h
|
+---geomutils
| GuContactBuffer.h
| GuContactPoint.h
|
+---gpu
| PxGpu.h
|
+---pvd
| PxPvd.h
| PxPvdSceneClient.h
| PxPvdTransport.h
|
+---solver
| PxSolverDefs.h
|
+---task
| PxCpuDispatcher.h
| PxTask.h
| PxTaskDefine.h
| PxTaskManager.h
|
\---vehicle
PxVehicleComponents.h
PxVehicleDrive.h
PxVehicleDrive4W.h
PxVehicleDriveNW.h
PxVehicleDriveTank.h
PxVehicleNoDrive.h
PxVehicleSDK.h
PxVehicleShaders.h
PxVehicleTireFriction.h
PxVehicleUpdate.h
PxVehicleUtil.h
PxVehicleUtilControl.h
PxVehicleUtilSetup.h
PxVehicleUtilTelemetry.h
PxVehicleWheels.h
我认为这些都是必需的头文件。然后,我添加了要链接的lib文件列表(我认为甚至其中的一些文件都是不必要的):
PhysX_static_64.lib PhysXPvdSDK_static_64.lib PhysXVehicle_static_64.lib PhysXCharacterKinematic_static_64.lib CPhysXExtensions_static_64.lib PhysXCooking_static_64.lib PhysXCommon_static_64.lib PhysXFoundation_static_64.lib SnippetUtils_static_64.lib SnippetRender_static_64.lib
然后我创建了一个非常简单的C ++代码来测试它是否有效:
#include "PxPhysicsAPI.h" int main() { auto Allocator = physx::PxDefaultAllocator(); auto ErrorCallback = physx::PxDefaultErrorCallback(); auto Foundation = PxCreateFoundation(PX_PHYSICS_VERSION, Allocator, ErrorCallback); auto PhysXVisualDebugger = PxCreatePvd(*Foundation); const auto transport = physx::PxDefaultPvdSocketTransportCreate("127.0.0.1", 5425, 10); PhysXVisualDebugger->connect(*transport, physx::PxPvdInstrumentationFlag::eALL); auto Physics = PxCreatePhysics(PX_PHYSICS_VERSION, *Foundation, physx::PxTolerancesScale(), true, PhysXVisualDebugger); physx::PxSceneDesc sceneDesc(Physics->getTolerancesScale()); sceneDesc.gravity = physx::PxVec3(0.0f, -9.81f, 0.0f); auto Dispatcher = physx::PxDefaultCpuDispatcherCreate(2); sceneDesc.cpuDispatcher = Dispatcher; sceneDesc.filterShader = physx::PxDefaultSimulationFilterShader; auto Scene = Physics->createScene(sceneDesc); auto pvdClient = Scene->getScenePvdClient();; pvdClient->setScenePvdFlag(physx::PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true); pvdClient->setScenePvdFlag(physx::PxPvdSceneFlag::eTRANSMIT_CONTACTS, true); pvdClient->setScenePvdFlag(physx::PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true); }
而且...这根本不起作用。当我尝试编译我的简单项目时,总是会从链接器中收到此错误:听起来可能很有趣,但是我几乎尝试了所有事情。我知道哪一行会引起此错误,因为如果删除调用
PxCreatePhysics
,它将编译而没有任何问题。只有这一行在链接方面有一些奇怪的问题,我不理解。我应该如何解决这个问题?我什至不知道在哪里寻找答案。我已经使用相同的编译器和相同的机器为正确的CPU体系结构和正确的运行时库编译了PhysX库,为什么Nvidia的示例可以工作而我的代码不能工作?
PX_PHYSX_STATIC_LIB
或者您可以在其他导入之前包含PxConfig.h
头文件,但我不建议使用此方法,因为IDE会一直通知您,此导入是不必要的(一点都不正确)。