问题
I'm trying to implement PhysX into my game engine, but I have got some weird problems with linking the PhysX library. It always fails no matter what I do, but snippets from Nvidia works like a charm. I will try to describe what I did and I hope someone will find what I'm missing.
First of all, I downloaded PhysX 4.1 from Github. Then I changed buildtools settings to those:
<?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>
Then I used bat script to generate Visual Studio 2017 project for static linking and for runtime static libraries. Then I compiled the project in debug and release version. SnippestHelloWorld works without any problems. So far so good. So I created new project in Visual Studio 2017 and I selected x64 processor architecture (like in PhysX xml configuration). Then I included header files the same way as in SnippestHelloWorld. Here is a tree:
| 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
I think those are all required header files. Then I added this list of lib files for linked (even some of them are unnecessary in my opinion):
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
And then I created a really simple C++ code to test if it works:
#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);
}
And... It just doesn't work. I always get this error from linker when I try to compile my simple project:
It might sound funny, but I tried almost everything. I event know which line couses this error, becuase if I remove calling PxCreatePhysics
it compiles without any problems. Only this one line has got some weird problem with linking and I do not understand it.
What should I try to resolve this? I do not even know where to look for an answer. I have got compiled PhysX library compiled for correct CPU Architecture and for correct Runtime libraries, with the same sompiler and with the same machine, why examples by Nvidia works and my code doesn't?
回答1:
I found the answer to my question. You need to add this preprocessor definition:
PX_PHYSX_STATIC_LIB
Or you can include PxConfig.h
header file before other imports, but I wouldn't recommend this method, because IDE will be informing you all the time, that this import is unnecessary (which is not true at all).
来源:https://stackoverflow.com/questions/58189023/physx-linking-problem-with-come-functions-imp-pxcreatebasephysics-reference