Reputation: 25
c++ compiler: default mingw from sourceforge
code editor: vscode
myFileStructure:
*build/
├── _deps/
│ ├── raylib-build/
│ ├── raylib-src/
│ │ ├── CHANGELOG
│ │ ├── include/
│ │ ├── lib/
│ │ ├── LICENSE
│ │ └── README.md
│ └── raylib-tmp/
├── .cmake/
│ └── api/
│ └── v1/
├── cmake_install.cmake
├── CMakeCache.txt
├── CMakeFiles/...morehere
├── compile_commands.json
└── Makefile
CMakeLists.txt
main.cpp*
main.cpp file:
#include "raylib.h"
int main()
{
// Initialization
InitWindow(800, 600, "Hello Raylib");
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose())
{
// Update
// Draw
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Hello, Raylib!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// De-Initialization
CloseWindow();
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.30)
project(myproject)
include(FetchContent)
set(RAYLIB_VERSION 5.0)
FetchContent_Declare(
raylib
URL https://github.com/raysan5/raylib/releases/download/5.0/raylib-5.0_win64_mingw-w64.zip
)
FetchContent_MakeAvailable(raylib)
# Include the raylib headers
include_directories(${raylib_SOURCE_DIR}/include)
# Link the raylib library
add_executable(main main.cpp)
target_link_libraries(main raylib)
running cmake .. output:
--downloades raylib here--
-- [download 98% complete]
-- [download 99% complete]
-- [download 100% complete]
-- Configuring done (3.7s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/admin/Desktop/projects/kui c++/main/build
running mingw32-make output:
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[100%] Linking CXX executable main.exe
CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:6: undefined reference to `InitWindow'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:7: undefined reference to `SetTargetFPS'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:10: undefined reference to `WindowShouldClose'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:15: undefined reference to `BeginDrawing'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:16: undefined reference to `ClearBackground'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:17: undefined reference to `DrawText'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:22: undefined reference to `CloseWindow'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\main.dir\build.make:98: recipe for target 'main.exe' failed
mingw32-make[2]: *** [main.exe] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/main.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:89: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
I was trying to link and get raylib to work with c++. In a tutorial I found it was recommended to use CMake, however after following the tutorial steps I still got the error messages about undefined references. How to modify the project to get rid of them?
Upvotes: 1
Views: 152
Reputation: 8312
In FetchContent_Declare
, you can change the link to point to the sources rather than to the released version for the specific platform:
FetchContent_Declare(
raylib
URL https://github.com/raysan5/raylib/archive/refs/tags/5.0.zip
)
This of course will cause the raylib
library to be compiled along with your project, but from the documentation of FetchContent_Declare I believe that this is the purpose of this command.
Another way would be to simply download the precompiled released version for your platform, install it, and then use find_package
CMake call to use it.
Upvotes: 1