MitchellSternke / SuperMarioBros-C

An attempt to translate the original Super Mario Bros. for the NES to readable C/C++
625 stars 70 forks source link

Undefined reference to `SDL_*` #8

Closed Mario0051 closed 5 years ago

Mario0051 commented 6 years ago

What do I do to fix this? I am compiling with mingw32-make.

[ 47%] Built target codegen
[ 52%] Linking CXX executable smbc.exe
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x120): undefined reference to `SDL_Init'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x15e): undefined reference to `SDL_CreateWindow'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x17f): undefined reference to `SDL_CreateRenderer'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x1a0): undefined reference to `SDL_RenderSetLogicalSize'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x1c8): undefined reference to `SDL_CreateTexture'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x21f): undefined reference to `SDL_OpenAudio'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x229): undefined reference to `SDL_PauseAudio'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x241): undefined reference to `SDL_CloseAudio'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x250): undefined reference to `SDL_DestroyTexture'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x25f): undefined reference to `SDL_DestroyTexture'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x26e): undefined reference to `SDL_DestroyRenderer'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x27d): undefined reference to `SDL_DestroyWindow'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x282): undefined reference to `SDL_Quit'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x2d6): undefined reference to `SDL_GetTicks'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x2ff): undefined reference to `SDL_PollEvent'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x345): undefined reference to `SDL_GetKeyboardState'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x529): undefined reference to `SDL_SetWindowFullscreen'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x569): undefined reference to `SDL_UpdateTexture'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x578): undefined reference to `SDL_RenderClear'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x592): undefined reference to `SDL_RenderSetLogicalSize'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x5b4): undefined reference to `SDL_RenderCopy'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x5ce): undefined reference to `SDL_RenderSetLogicalSize'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x5f0): undefined reference to `SDL_RenderCopy'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x5ff): undefined reference to `SDL_RenderPresent'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x604): undefined reference to `SDL_GetTicks'
CMakeFiles\smbc.dir/objects.a(Main.cpp.obj):Main.cpp:(.text+0x658): undefined reference to `SDL_Delay'
CMakeFiles\smbc.dir/objects.a(APU.cpp.obj):APU.cpp:(.text+0x39f): undefined reference to `SDL_LockAudio'
CMakeFiles\smbc.dir/objects.a(APU.cpp.obj):APU.cpp:(.text+0x49a): undefined reference to `SDL_UnlockAudio'
CMakeFiles\smbc.dir/objects.a(Video.cpp.obj):Video.cpp:(.text+0x3fb): undefined reference to `SDL_CreateTexture'
CMakeFiles\smbc.dir/objects.a(Video.cpp.obj):Video.cpp:(.text+0x4f8): undefined reference to `SDL_SetTextureBlendMode'
CMakeFiles\smbc.dir/objects.a(Video.cpp.obj):Video.cpp:(.text+0x516): undefined reference to `SDL_UpdateTexture'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\smbc.dir\build.make:198: smbc.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:72: CMakeFiles/smbc.dir/all] Error 2
mingw32-make: *** [Makefile:83: all] Error 2
MitchellSternke commented 5 years ago

This looks like a linking problem. Can you tell me what version of SDL you have installed?

I also use mingw32-make on my Windows system, so this should work if configured properly.

Mario0051 commented 5 years ago

I am using SDL 2.0.8.

MitchellSternke commented 5 years ago

Okay. That version should work.

Can you tell me where you have the library files installed for SDL2 on the machine?

Also, can you add the following code to the end of CMakeLists.txt (after the line with add_dependencies(smbc codegen)) at the root of the repo and re-run cmake? This should print out the variables that cmake finds, and help us figure out where it thinks the SDL libraries are located on your system. Ideally the value SDL2_LIBRARY or SDL2_LIBRARIES should be defined.

function(echo_all_cmake_variable_values)
  message(STATUS “”)
  get_cmake_property(vs VARIABLES)
  foreach(v ${vs})
    message(STATUS “${v}=’${${v}}'”)
  endforeach(v)
  message(STATUS “”)
endfunction()

echo_all_cmake_variable_values()

Please let me know what the output of cmake is after you add this code to CMakeLists.txt. Hopefully this will help us figure out what is going on 😄

Mario0051 commented 5 years ago

SDL2_LIBRARY is defined as: "C:\Users\*****\Downloads\SDL2-2.0.8\lib\x64"

MitchellSternke commented 5 years ago

On my system I see SDL2_LIBRARY is equal to mingw32C:/TDM-GCC-64/lib/libSDL2main.aC:/TDM-GCC-64/lib/libSDL2.dll.a. So I think in your case it needs to point to the correct library files (.lib or .a) and not just the directory (x64). Something with the SDL2 detection logic must not be working properly. I have had issues with this before 😞

What library files are installed in the x64 directory? I think if you are compiling with mingw you will likely need the GNU style libraries (they should have a *.a file extension) and not the MSVC onces (these would have a *.lib file extension).

If you have the incorrect libraries I would download this exact file from SDL's webpage, as it should have what you need (this is SDL 2.0.8 for mingw specifically): direct link download page -- you want SDL2-devel-2.0.8-mingw.tar.gz

Hope this helps!

Mario0051 commented 5 years ago

I do have the MinGW ones, I will start using them now. Hopefully it will work.

Mario0051 commented 5 years ago

EDIT: I haven't tried directly linking SDL2_LIBRARY to the *.a file.

Mario0051 commented 5 years ago

I tried linking it and it gives me this error, even though the file exists: G__~1.EXE: error: C:Users*****DownloadsSDL2-2.0.8x86_64-w64-mingw32liblibSDL2.dll.a: No such file or directory

MitchellSternke commented 5 years ago

That's strange. Maybe try copying the library files to the /lib folder of your MinGW installation? Personally that is where I have the SDL2 libraries installed on my system.

Mario0051 commented 5 years ago

I have forgotten to change SDL2_INCLUDE_DIR...and I didn't need to include SDL2_LIBRARY in my cmake .. command. It seems to compile fine now.

Mario0051 commented 5 years ago

When I run the program, it displays nothing and crashes immediately. EDIT: Compiled from fresh code and it seems to work fine. I had to manually enter the SDL.h location inside the code but compiled flawlessly. Is it supposed to look like this?: image EDIT2: There is little to no sound at all, and the sound that comes out is just popping noises. EDIT3: Made a new issue for this specific issue.

apavlyuc commented 4 years ago

@Mario0051 , could you share your cmake file with me? I have the same problem, but still can't resolve.

Mario0051 commented 4 years ago

Actually all you need to do is to replace the path in between the <> in the files APU.cpp, Main.cpp, and Video.hpp with where your SDL.h is located: #include <SDL2/SDL.h>

For example: #include <C:/Users/*****/Downloads/SDL2-2.0.9/x86_64-w64-mingw32/include/SDL2/SDL.h>

segin commented 3 years ago

Actually all you need to do is to replace the path in between the <> in the files APU.cpp, Main.cpp, and Video.hpp with where your SDL.h is located: #include <SDL2/SDL.h>

For example: #include <C:/Users/*****/Downloads/SDL2-2.0.9/x86_64-w64-mingw32/include/SDL2/SDL.h>

You should NEVER hardcode local paths! Also, just use #include <SDL.h>, if it gives you problems, you're not properly using sdl2-config.