SDL Error Undefined symbols for architecture x86_64 "_SDL_main" - c++

I am using C++ with the SDL Cocoa and Foundation framework on my mac os x. I get the following error
Undefined symbols for architecture x86_64:
"_SDL_main", referenced from:
-[SDLMain applicationDidFinishLaunching:] in SDLMain.o
ld: symbol(s) not found for architecture x86_64
when I run the following code
#import <Foundation/Foundation.h>
#import <SDL/SDL.h>
#include "SDLMain.h"
int main(int argc, const char * argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF);
SDL_Event event;
bool isRunning = true;
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
isRunning=false;
}
}
SDL_Quit();
return 0;
}
I have no idea what is wrong, although it seems that when I go into the SDLMain.m file and comment out this line of code
status = SDL_main (gArgc, gArgv);
the program compiles with no problems. However, it doesn't work. No window opens like its supposed to. Any ideas?

I bet your main function signature is incorrect. You use:
int main(int argc, const char * argv[])
^^^^^
but SDL_main.h wants
int main(int argc, char *argv[])
Why?
You see, SDL does something really horrific when compiling: It renames your main function to SDL_main, injecting its own main function which, in turn, calls yours.
Note that if this doesn't work, then you may be compiling with wrong flags. To be sure, get the flags by typing:
$ sdl-config --cflags --libs
For more information, see Simply including SDL header causes linker error

I had the same problem, even though my main signature was correct. I Found the answer here: SDL in XCode 4.3.2 SDLMain.o undefined symbols
Turned out the problem was that I added SDLMain.m and SDLMain.h
(according to a suggestion on the libSDL website about OS X
frameworks) to an existing SDL project which messed up with main.
Bottom line is that you don't need those files just because you are
using Cocoa -- SDL's own test apps don't use it either and run just
fine on OS X.

Related

Boost.Test - Undefined symbol when overriding main

I want to provide my own main function while using Boost.Test. So I have included the following macros:
#define BOOST_TEST_ALTERNATIVE_INIT_API
#define BOOST_TEST_NO_MAIN
My main function looks like this:
int main(int argc, char* argv[])
{
int exitCode = ::boost::unit_test::unit_test_main(&initialise, argc, argv);
return exitCode;
}
I have also created the function initialise.
When built on OS X using Xcode 6 the following error is reported:
Undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
_main in main.o
I am linking-in the unit test framework library.
Can someone please help resolve the error?
There are two possible solutions:
1) Include <boost/test/included/unit_test.hpp> in the test module. This has the effect of building both the test framework and the test module simultaneously.
2) Rebuild the test framework library withBOOST_TEST_NO_MAINand BOOST_TEST_ALTERNATIVE_INIT_API.
According to the documentation the 2nd option is preferred.

XCode "Undefined symbols for architecture x86_64" after linking C library in C++ program

I'm trying to link to libav (which I compiled on the same system (Mac 10.8)), with a C++11 program (here stripped down to minimum size reproducing the error):
#include "libavformat/avformat.h"
int main(int argc, const char * argv[]) {
av_register_all();
return 0;
}
Here is a screenshot of the link error, notice on the right in the second last line in the command where I've included "-lavcodec -lavformat -lavutil" which reside as lib*.a files in /usr/local/lib.
There are no issues when I rename the source file program to *.c, such that XCode compiles it as a C program, but I would like to use C++11.

Compiling C++ with SDL on Mac OS X Lion

I thought Ubuntu and OS X would have similar interfaces for compiling a C/C++ program with SDL but nothing I am trying, or finding on Google seems to be working.
I have found a solution for Xcode, but I am not using Xcode. I am writing my programs from Sublime Text 2 and compiling via command-line since I prefer it and work much faster that way.
Long story short, I am receiving an array of errors with each attempt and thus far I have copied SDL.framework into my /Library/Frameworks directory.
This is the closest I have gotten to actually compiling:
[ 674 / 174 / 0 ] $ compcpsdl
Undefined symbols for architecture x86_64:
"_SDL_main", referenced from:
-[SDLMain applicationDidFinishLaunching:] in ccYYA0Ea.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Terminal command (i.e. compcpsdl)
g++ -I/Library/Frameworks/SDL.framework/Headers main.cpp SDLmain.m -framework SDL -framework Cocoa
With the following project structure:
Project-Directory/
--- main.cpp
--- SDLMain.m
--- SDLMain.h
And lastly, the following source code:
#include <iostream>
#include <SDL/SDL.h>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
It appears I'm doing everything correctly, yet nothing seems to be working.
Any ideas guys?
I Think SDL does sometimes a bit strange stuff with the main function it assumes the mainfunction is defined in one way. Notice that I added int argc and char** argv to the definition of you main function.
Try:
int main (int argc, char** argv)
{
cout << "Hello World!";
return 0;
}
I encountered this problem also once and it was quite mystifying.
for more info see this stackoverflow question: Why SDL defines main macro?

Code::Blocks, MinGW, libsdl, and GNU C++ compiler: undefined reference to `WinMain#16

I have been trying to compile the most basic SDL application, but no matter what I do I keep getting this error:
c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain#16'
I searched for solutions for this, but they all had to do with either Visual C++ or a missing main. I am not using Visual C++, and I have defined main.
Here's my code:
#include "SDL/SDL.h"
int main( int argc, char* args[] )
{
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
}
Don't use "Other linker options". Use the "Link libraries" section. Add the following items.
mingw32
SDLmain
SDL
You can put -mwindows in the "Other linker options" section.
In case someone else comes across this, I put -lmingw32 after -lSDLmain and -lSDL which caused this issue for me. Putting -lmingw32 first fixed it.
I encountered the same error in a project of mine that I want to compile both on linux and windows.
I use a makefile to compile the project.
A solution that has worked for me, although I admit it is a bit of a hack is adding this to main.cpp (wherever your main function would be)
extern "C" {
int WinMain(int argc, char** argv)
{
return main(argc, argv);
}
}
This makes the linker find WinMain and use it as the entry point in the program. I can also hope that this solution doesn't break linux compilability, hopefully it will be considered just an unused function.

Having trouble using SDL framework on XCode

I am having trouble using the SDL framework in my xcode project, my main (all there is in the project at the moment) currently looks like this:
#include <SDL/SDL.h>
#include <iostream>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
And the error I am receiving when building is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: __Z8SDL_mainiPPKc)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It does not give me an error about the framework not being found. and I have looked around Stackoverflow for this problem and I did find one person who had their framework in the wrong place on their OS. But I have tried all places (/library/frameworks && /~username~/library/frameworks && /system/library/frameworks) and still no luck.
Additional info:
I did however notice after some searching on the internet that the official website http://www.libsdl.org/download-1.2.php
Does not have a OSX version of the Development library. While many tutorials on how to use SDL on OSX say these are required.
Also I am adding my library via Xcode itself ,not through drag'ndrop. Xcode seems to reckognize it.
Would be much appreciated if anyone could help, going crazy over this error.
UPDATE:
Still no luck, I have followed every step provided by solutions here below. perhaps this screenshot is of any help.
The main() function is corrected, but didn't help. I tried changing the path to the header files, im lost on this one. Does anyone perhaps have any alternatives to building this in xcode?
UPDATE2:
The suggestion worked, however now it is giving me this warning, which won't let me run the application.
Fixed! I removed the path in the build settings. Strange how I still don't know what went wrong, either way. thanks a lot for the help! made my day!
I see three possible problems. The first problem is how you're including SDL.h in your code. You should include SDL.h with the following code:
#include "SDL.h"
The second possible problem is you haven't added the files SDLMain.h and SDLMain.m to your project. Those files are necessary to compile SDL code on Mac OS X. The files are in the devel-lite folder on the SDL disk image.
The third possible problem is that your project doesn't link to the Cocoa framework. The Mac version of SDL uses Cocoa so you need the Cocoa framework in your project.
The following article walks you through the setup of an Xcode 4 project for SDL:
Using SDL with Xcode 4
UPDATE
I noticed a possible problem in how you defined the main() function. You have a space between char and * and another space between * and argv, which could be causing the error. The main() function in my SDL code is defined in the following way:
int main(int argc, char *argv[])