1

我正在尝试在我的游戏引擎中实现 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项目。然后我在调试发布版本中编译了项目。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 库,使用相同的 sompiler 和相同的机器,为什么 Nvidia 的示例有效而我的代码无效?

4

1 回答 1

0

我找到了我的问题的答案。您需要添加此预处理器定义:

PX_PHYSX_STATIC_LIB

或者你可以PxConfig.h在其他导入之前包含头文件,但我不推荐这种方法,因为 IDE 会一直通知你,这个导入是不必要的(这根本不是真的)。

于 2019-10-02T19:03:17.863 回答