Raygui and cmake with multiple targets

3 days ago 8
ARTICLE AD BOX

I am having some trouble to configuring raygui.h with cmake using two targets, a library and a executable. There is a repository here with the exact files that causes errors of multiple definition of SomeFunction during the build. The errors are like below:

/usr/bin/ld: libgame-lib.a(hello_world.cpp.o): in function `GuiEnable': hello_world.cpp:(.text+0x0): multiple definition of `GuiEnable'; CMakeFiles/exe.dir/src/main.cpp.o:main.cpp:(.text+0x0): first defined here

It's like raygui.h declaration were all made twice.

The problem does not occurs in the master branch, where only one target is linked directly with raygui.h.

These are the files in the branch that has the problem:

// hello_world.hpp // ---------------------------------------------------------------------------- #include "raylib.h" #define RAYGUI_IMPLEMENTATION #include "raygui.h" void hello_world(); // ---------------------------------------------------------------------------- // hello_world.cpp // ---------------------------------------------------------------------------- #include "hello_world.hpp" void hello_world() { InitWindow(300, 300, "Hello World with raylib."); while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(BLACK); DrawText("Hello World", 120, 150, 12, RED); GuiButton((Rectangle) {115, 180, 80, 16}, "Hello World"); EndDrawing(); } } // ---------------------------------------------------------------------------- // main.cpp // ---------------------------------------------------------------------------- #include "hello_world.hpp" int main() { hello_world(); return 0; } // ---------------------------------------------------------------------------- # CMakeLists.txt # ---------------------------------------------------------------------------- cmake_minimum_required(VERSION 3.22.0) project(hello_world) include(FetchContent) # Generate compile_commands.json set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Dependency: raylib set(RAYLIB_VERSION 5.0) FetchContent_Declare( raylib DOWNLOAD_EXTRACT_TIMESTAMP OFF URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz FIND_PACKAGE_ARGS ) FetchContent_MakeAvailable(raylib) # Dependency: raygui (no root CMakeList.txt) set(RAYGUI_VERSION 4.0) FetchContent_Declare( raygui DOWNLOAD_EXTRACT_TIMESTAMP OFF URL https://github.com/raysan5/raygui/archive/refs/tags/${RAYGUI_VERSION}.tar.gz FIND_PACKAGE_ARGS ) FetchContent_MakeAvailable(raygui) add_subdirectory("${raygui_SOURCE_DIR}/projects/CMake") # raygui has a lot of warnings of -Wenum-compare add_compile_options(-Wno-enum-compare) # library set(LIBRARY_NAME game-lib) add_library(${LIBRARY_NAME} "${CMAKE_SOURCE_DIR}/src/game-lib/hello_world.cpp" "${CMAKE_SOURCE_DIR}/src/game-lib/hello_world.hpp" ) target_link_libraries(${LIBRARY_NAME} PUBLIC raylib raygui ) target_include_directories(${LIBRARY_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/src/game-lib") # executable set(EXECUTABLE_NAME exe) add_executable(${EXECUTABLE_NAME} "${CMAKE_SOURCE_DIR}/src/main.cpp" ) target_link_libraries(${EXECUTABLE_NAME} PUBLIC ${LIBRARY_NAME}) set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PROJECT_NAME}) set_property(TARGET ${EXECUTABLE_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>) # ----------------------------------------------------------------------------
Read Entire Article