1

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?

2
  • 1
    If target_link_libraries(main raylib) is correct, then include_directories(${raylib_SOURCE_DIR}/include) should not be needed, because raylib target already has correct include dirs in the properties. But I'm not sure how FetchContent_MakeAvailable is dealing with these things. What if you simply unpack the raylib zip in your project and add_subdirectory(raylib-5.0) ? Commented Sep 12, 2024 at 15:05
  • default mingw from sourceforge naah, get mingw from msys2.org , the version you got from other sites is like over 10 years old Commented Sep 12, 2024 at 16:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

doing that started compiling raylib as you sayed but it threw this conflict error at the end ``` In file included from c:\mingw\include\windows.h:48:0, from C:\Users\admin\Desktop\projects\kui c++\main\build_deps\raylib-src\src\raudio.c:136: c:\mingw\include\winuser.h:3519:25: error: conflicting types for 'CloseWindow' WINUSERAPI BOOL WINAPI CloseWindow (HWND); ^~~~~~~~~~~ In file included from C:\Users\admin\Desktop\projects\kui c++\main\build_deps\raylib-src\src\raudio.c:75:0: and many like this i dont have space in this comment
@KetsebaotGizachew looks similar to your problem github.com/raysan5/raylib/issues/792 , you likely need an updated 64bit version of mingw to compile raylib, get an updated version of mingw from msys2.org , don't forget to uninstall the version you currently have on your system to not end up with conflicts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.