Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL_MAIN_USE_CALLBACKS & macOS => linker error, undefined symbol: _main #11857

Open
Robert-M-Muench opened this issue Jan 5, 2025 · 11 comments
Assignees
Labels
waiting Waiting on user response
Milestone

Comments

@Robert-M-Muench
Copy link

I get the following link error:

====================[ Build | sdl | Debug (macOS/Arm64) ]=======================
/Users/robby/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/cmake --build /Users/Shared/develop/rm-asset/c-code/playground/sdl/cmake-build-debug-macos-arm64 --target sdl -j 14
[2/2] Linking CXX executable sdl
FAILED: sdl 
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lpthread CMakeFiles/sdl.dir/main.c.o CMakeFiles/sdl.dir/svg_c_wrapper.cpp.o -o sdl  _deps/blend2d-build/libblend2d.a  _deps/sdl3-build/libSDL3.a  _deps/sdl3_image-build/libSDL3_image.a  -lc  -lm  -framework CoreMedia  -framework CoreVideo  -framework Cocoa  -framework IOKit  -framework ForceFeedback  -framework Carbon  -framework CoreAudio  -framework AudioToolbox  -framework AVFoundation  -framework Foundation  -Xlinker -weak_framework -Xlinker GameController  -Xlinker -weak_framework -Xlinker Metal  -Xlinker -weak_framework -Xlinker QuartzCore  -framework CoreHaptics  -Xlinker -weak_framework -Xlinker UniformTypeIdentifiers  -Wl,-framework,ApplicationServices  -lobjc && :
Undefined symbols for architecture arm64:
  "_main", referenced from:
      <initial-undefines>
ld: symbol(s) not found for architecture arm64
c++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Am I missing something?

@icculus
Copy link
Collaborator

icculus commented Jan 5, 2025

Did you include SDL_main.h after defining SDL_MAIN_USE_CALLBACKS?

Like this:

#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

@icculus icculus added this to the 3.2.0 milestone Jan 5, 2025
@icculus icculus self-assigned this Jan 5, 2025
@Robert-M-Muench
Copy link
Author

Robert-M-Muench commented Jan 5, 2025

Yes, and there seems to be a problem with the macros. Right after the SDL_main.h include the defines have the following value:

  • SDL_MAIN_HANDLED: 1
  • SDL_Main: SDL_Main
  • SDL_MAIN_AVAILABLE: SDL_MAIN_AVAILABLE

The last two are undefined, that's what the string literal is printed by #pragma message

Adding the following code to main.c file solves the problem:

int SDL_main(int argc, char **argv) {
	return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit);
}

#undef main
int main(int argc, char *argv[]) {
	(void)argc;
	(void)argv;
	return SDL_RunApp(0, NULL, SDL_main, NULL);
}

But, of course, that's not how it's intended.

@icculus
Copy link
Collaborator

icculus commented Jan 6, 2025

c++: error

Hmm...I wonder if we got bitten by name-mangling here. Let me see if I can reproduce this over here.

@Robert-M-Muench
Copy link
Author

My code is a mixture of C and C++. Hence, the C++ linker kicks in.

However, I think it's a macro bug on the APPLE platform because the file where the link error is reported is a C file.

I looked at the handling of main.h/main_impl.h/platform_defines.h, and it doesn't look very easy. It's a convoluted mess of fragmented macro definitions across several files, and several must meet specific conditions for things to work.

Maybe adding much more compile time message output and checks with erroring out is the best first step to hunt down this problem.

@icculus
Copy link
Collaborator

icculus commented Jan 6, 2025

Okay, wait, rereading this again, why is SDL_MAIN_HANDLED defined? Did your app define that somewhere, or does your app have SDL_WIKI_DOCUMENTATION_SECTION defined for some reason?

Stick this in before the #include <SDL3/SDL_main.h> line:

#ifdef SDL_MAIN_HANDLED
#error SDL_MAIN_HANDLED shouldn't be defined here!
#endif
#ifdef SDL_WIKI_DOCUMENTATION_SECTION
#error SDL_WIKI_DOCUMENTATION_SECTION shouldn't be defined, ever!
#endif
#include <SDL3/SDL_main.h>

@Robert-M-Muench
Copy link
Author

Robert-M-Muench commented Jan 6, 2025

I didn't define SDL_MAIN_HANDLED anywhere nor do I use SDL_WIKI_...

Adding the pre-processor stuff before the #include <SDL3/SDL_main.h> triggers the SDL_MAIN_HANDLED shouldn't be defined here! error.

@icculus
Copy link
Collaborator

icculus commented Jan 6, 2025

Okay, so we have to figure out where that is getting defined, because it's not defined by SDL's headers (except in that wiki documentation section, which is why we checked that here, too).

Scour your build scripts, grep in the project, etc?

@madebr
Copy link
Contributor

madebr commented Jan 6, 2025

From README-cmake.md and the example template, does this CMakeLists.txt configure and build?

CMakeLists.txt
(Download it to a temporary directory, and configure/build it as usual)

@Robert-M-Muench
Copy link
Author

Works.

@icculus
Copy link
Collaborator

icculus commented Jan 7, 2025

It has to be somewhere in your project files, or something your project is picking up, like some external configuration thing that's gone wrong.

If just straight-up grepping for SDL_MAIN_HANDLED in your project doesn't find the culprit, you can try running ninja -v from the command line when you build your project, and it'll show all the compiler command lines; see if there's an obvious place where it inserted this when building the file with main() in it. Otherwise, I'd look at where you're pulling in SDL3 from (homebrew or something package management thing?) and see if they incorrectly added that flag to the build in some way.

@icculus icculus added the waiting Waiting on user response label Jan 11, 2025
@icculus
Copy link
Collaborator

icculus commented Jan 11, 2025

Kicking this out of the milestone for now, but I'm still under the impression this isn't our bug, so I'm probably closing this soon unless I hear otherwise.

@icculus icculus modified the milestones: 3.2.0, 3.4.0 Jan 11, 2025
@slouken slouken modified the milestones: 3.4.0, 3.x Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting Waiting on user response
Projects
None yet
Development

No branches or pull requests

4 participants