[SOLVED] Error building Panda3D using CMake and Clang

Hello, very new user here. So, I decided to code in C++ instead of Python. I also used Clang, CMake, and Ninja for the project instead of Visual Studio 2022. I installed and followed the instructions for installing Panda3D on Windows, and it was successful. The problem arose when I started building my project. The error, specifically a linker error, that was thrown is:

PS ~\Projects\C++\HelloPanda3D\build> cmake --build .
[2/2] Linking CXX executable game.exe
FAILED: game.exe 
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\msys64\ucrt64\bin\clang++.exe -fPIC -O2  CMakeFiles/game.dir/main.cpp.obj -o game.exe -Wl,--out-implib,libgame.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/Panda3D-1.10.14-x64/lib -lp3framework  -lpanda  -lpandafx  -lpandaexpress  -lp3dtoolconfig  -lp3dtool  -lp3direct  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0x31): undefined reference to `PandaFramework::PandaFramework()'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0x47): undefined reference to `PandaFramework::open_framework(int&, char**&)'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0xd5): undefined reference to `PandaFramework::open_window()'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0xde): undefined reference to `PandaFramework::main_loop()'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0xe7): undefined reference to `PandaFramework::close_framework()'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0xf0): undefined reference to `PandaFramework::~PandaFramework()'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.text+0x123): undefined reference to `PandaFramework::~PandaFramework()'
C:/msys64/ucrt64/bin/ld: CMakeFiles/game.dir/main.cpp.obj:main.cpp:(.rdata$.refptr.panda_version_1_10[.refptr.panda_version_1_10]+0x0): undefined reference to `panda_version_1_10'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

This was puzzling, because Panda3D’s path is in the System Environment Variable. The directories are also proper. Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(HelloPanda3D)

# Set the Panda3D include and library directories
set(PANDA3D_INCLUDE_DIR "C:/Panda3D-1.10.14-x64/include")
set(PANDA3D_LIB_DIR "C:/Panda3D-1.10.14-x64/lib")

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -O2")

# Include directories
include_directories(${PANDA3D_INCLUDE_DIR})

# Link directories
link_directories(${PANDA3D_LIB_DIR})

# Add the executable
add_executable(game main.cpp)

# Link Panda3D libraries
target_link_libraries(game
    p3framework
    panda
    pandafx
    pandaexpress
    p3dtoolconfig
    p3dtool
    p3direct
)

I’m not sure if the library names are correct, though (I tried, for example, libp3framework, but it still didn’t work anyway). Thanks in advance.

In Windows, only the Microsoft compiler is suitable for building an application

1 Like

So, is the Visual Studio Build Tools enough, or do I still need to install the full Visual Studio 2022 IDE?

You don’t need the full IDE, if you can install just the compilers and Windows SDK. It’s possible to use clang as well, but I think it has to be the clang that ships with the Visual Studio tools.

1 Like

I think in order to use clang, you need to build Panda3D libraries using it. However, it needs to be checked, I’ve never thought about it.

I have another issue. I got to successfully run my project with MSVC as my compiler, and I still used CMake and VS Code. However, when I ran the project with the ā€œA Panda3D Hello World Tutorialā€ example code (the final one with the moving Panda in a grassy scenery), it threw an error. This is what it showed:

Although I can click the ā€˜Ignore’ option and proceed, this ā€œAssertion failedā€ error might cause more problems in the future. Is there any way to fix this?

Have you followed the instructions about which preprocessor macros you should set (neither _DEBUG nor NDEBUG for the public build!)?

Otherwise, do you have the stack trace for this? You should be able to get a debugger up when you press Retry.

I’m not sure about the macros you are talking about. I did not set any macros in my CMakeLists.txt or main.cpp files. I tried to debug it from VS Code’s debugger as well, and the breakpoint is at around:

taskMgr->add(new GenericAsyncTask("Spins the camera", &SpinCameraTask, nullptr));

Also, is building Panda3D really required? I got to install it through the Windows installer without any problems, that’s why I was able to build and run my project with MSVC and CMake.

Well, check which macros CMake is setting by default in your configuration. It’s likely setting _DEBUG if you’re building in Debug configuration and NDEBUG in Release configuration. You’ll want to change that. You probably want to build in Release configuration and remove the NDEBUG setting.

You don’t need to build Panda3D from source, no.

Oh, right. I checked that one, and I found out that when building with MSVC and CMake, it defaults to Debug when no flag is mentioned (defaults to, e.g., cmake --build build --config Debug). I simply changed it to RelWithDebInfo. Thanks for bringing that up.