0

Getting this error when I try compile.

/usr/bin/ld: src/engine/libengine.a(rengine.cpp.o): warning: relocation against `_ZN8REngine7mEngineE' in read-only section `.text'
/usr/bin/ld: CMakeFiles/sdlgame.dir/src/main.cpp.o: in function `REngine::engine()':
/media/hd/sdlgame/src/engine/rengine.h:27: undefined reference to `REngine::mEngine'
/usr/bin/ld: src/engine/libengine.a(rengine.cpp.o): in function `REngine::REngine()':
/media/hd/sdlgame/src/engine/rengine.cpp:8: undefined reference to `REngine::mEngine'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/sdlgame.dir/build.make:99: sdlgame] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:119: CMakeFiles/sdlgame.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:126: CMakeFiles/sdlgame.dir/rule] Error 2
gmake: *** [Makefile:124: sdlgame] Error 2

In my main function I call:

#include "engine/rengine.h"
#include <stdio.h>
#include <string>
#include <fstream>

int main( int argc, char* args[] )
{
    auto engine = REngine::engine(); 
    return 0;
}

and class REngine looks like this:

#include <SDL2/SDL.h>

class REngine
{
public:
    REngine() {}
    ~REngine() {}

    inline static REngine* engine() { 
        return mEngine = mEngine ? mEngine : new REngine; 
    }

private:
    static RREngine* mEngine;
};

The file system looks like this:

sdlgame/
    CMakeLists.txt
    src/
        main.cpp
        engine/
            CMakeLists.txt
            engine.cpp
            engine.h

My sdlgame/CMakeLists.txt looks like this

CMake_Minimum_Required (VERSION 3.1)
project (sdlgame VERSION 0.1.0)

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_mixer REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS} ${SDL2_MIXER_INCLUDE_DIRS})

add_subdirectory(src/engine)

add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC engine ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} ${SDL2_TTF_LIBRARIES} ${SDL2_MIXER_LIBRARIES})

and my sdlgame/src/engine/CMakeLists.txt looks like this:

file( GLOB engine_SOURCES *.cpp )
add_library( engine ${engine_SOURCES} )
target_include_directories(engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

So I did some research and it looks like it could be I haven't linked engine to my project properly, but I can use the engine class just fine and create a new object just fine as well. Would anyone have any other idea why this is happening?

On Ubuntu 22.04 (Kubuntu I guess) btw

Shizo
  • 5
  • 3
  • Where have you defined the constructor and destructor? Perhaps you can just `=default;` them or delete them alltogether. – Quimby Dec 13 '22 at 10:36
  • typo with REngine() and ~REngine()? – ByteTh1ef Dec 13 '22 at 10:38
  • Oops, constructor and destructor are defined in the cpp file. Should have mentioned that, edited the question to make it work. – Shizo Dec 13 '22 at 10:42
  • `REngine::mEngine` is static. It requires a definition somewhere. Where is it? – j6t Dec 13 '22 at 10:57
  • Ahh I see. I need to add ```REngine* REngine::mEngine = nullptr;``` to the top of my ```engine.cpp``` file. Seems hacky but also seems to like it. Thanks for the help, didn’t know static members need to be initialised but it makes sense. – Shizo Dec 13 '22 at 11:11

1 Answers1

0

I needed to add REngine* REngine::mEngine = nullptr; to the top of my engine.cpp file. REngine::mEngine is static and it requires a definition somewhere. Thanks to @Quimby, @ByteTh1ef, & @j6t.

Shizo
  • 5
  • 3