Search code examples
ccmakesdlclion

Is there an easier way to compile this project


Under Linux(Ubuntu) I'm writing a CMakeLists.txt with CLion to compile my project which is a little game written in C. How do I fix these errors?

Here's a link to the entire source code.

Any help will be greatly appreciated

I have tried to rewrite my .h files. It was primarily an issue about linking SDL and SDL_images.

I have tried to compile the main with gcc main.c -o main -lSDL -lSDL_image.

Here's the CMakeLists.txt

cmake_minimum_required(VERSION 3.7)

project(SOKOBAN)

set(CMAKE_C_STANDARD 99)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)

find_package(SDL REQUIRED)
find_package(SDL)
find_package(SDL_image)



set(SOURCE_FILES main.c jeu.c jeu.h editeur.c editeur.h
        fichiers.c fichiers.h Constantes.h  hsokoban.h)

add_executable(SOKOBANR ${SOURCE_FILES})

target_link_libraries(SOKOBANR SDL_image SDL)

The error is:

[ 20%] Linking C executable SOKOBANR
CMakeFiles/SOKOBANR.dir/jeu.c.o: In function `jouer':
/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:12: multiple definition of `jouer'
CMakeFiles/SOKOBANR.dir/main.c.o:/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:12: first defined here
/usr/bin/ld: skipping incompatible /home/mahamad/github/SOKOBAN/SOKOBAN/lib/libSDL.a when searching for -lSDL
CMakeFiles/SOKOBANR.dir/main.c.o: In function `jouer':
/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:72: undefined reference to `deplacerJoueur'
/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:76: undefined reference to `deplacerJoueur'
/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:80: undefined reference to `deplacerJoueur'
/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:84: undefined reference to `deplacerJoueur'
CMakeFiles/SOKOBANR.dir/jeu.c.o: In function `jouer':
/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:72: undefined reference to `deplacerJoueur'
CMakeFiles/SOKOBANR.dir/jeu.c.o:/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:76: more undefined references to `deplacerJoueur' follow
CMakeFiles/SOKOBANR.dir/fichiers.c.o: In function `sauvegarderNiveau':
/home/mahamad/github/SOKOBAN/SOKOBAN/fichiers.c:67: undefined reference to `fprint'
collect2: error: ld returned 1 exit status
CMakeFiles/SOKOBANR.dir/build.make:128: recipe for target 'SOKOBANR' failed
make[3]: *** [SOKOBANR] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/SOKOBANR.dir/all' failed
make[2]: *** [CMakeFiles/SOKOBANR.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/SOKOBANR.dir/rule' failed
make[1]: *** [CMakeFiles/SOKOBANR.dir/rule] Error 2
Makefile:118: recipe for target 'SOKOBANR' failed
make: *** [SOKOBANR] Error 2```

Solution

  • If you get such errors you need to look into each of them:


    CMakeFiles/SOKOBANR.dir/jeu.c.o: In function `jouer':
    /home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:12: multiple definition of `jouer'
    CMakeFiles/SOKOBANR.dir/main.c.o:/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:12: first defined here
    

    The linker thinks that you have (at least) two definitions of jouer. Since both referenced locations are the same I assume that you 1. include "jeu.c" instead of "jeu.h" in your "main.c", and 2. link both "main.o" and "jeu.o".


    /usr/bin/ld: skipping incompatible /home/mahamad/github/SOKOBAN/SOKOBAN/lib/libSDL.a when searching for -lSDL
    

    The library "libSDL.a" you provide is not compatible to the target system. Your target system seems to be Linux.

    For which system is the library?

    Did you compile it yourself or did you download it?


    CMakeFiles/SOKOBANR.dir/main.c.o: In function `jouer':
    /home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:72: undefined reference to `deplacerJoueur'
    /home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:76: undefined reference to `deplacerJoueur'
    /home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:80: undefined reference to `deplacerJoueur'
    /home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:84: undefined reference to `deplacerJoueur'
    CMakeFiles/SOKOBANR.dir/jeu.c.o: In function `jouer':
    /home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:72: undefined reference to `deplacerJoueur'
    CMakeFiles/SOKOBANR.dir/jeu.c.o:/home/mahamad/github/SOKOBAN/SOKOBAN/jeu.c:76: more undefined references to `deplacerJoueur' follow
    

    There are references (calls) of deplacerJoueur but you don't define it. Or the source file with the definition (implementation) is not added to the list of modules.


    CMakeFiles/SOKOBANR.dir/fichiers.c.o: In function `sauvegarderNiveau':
    /home/mahamad/github/SOKOBAN/SOKOBAN/fichiers.c:67: undefined reference to `fprint'
    

    This is clearly a typo. You mean fprintf() with a trailing 'f' for sure.


    collect2: error: ld returned 1 exit status
    

    Because of all the errors the linker is not successful, and it tells you.


    CMakeFiles/SOKOBANR.dir/build.make:128: recipe for target 'SOKOBANR' failed
    

    Because of all the errors the build is not successful, and it tells you.