Compiling C++ with SDL on Mac OS X Lion - c++

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?

Related

Compiling multiple files in Visual Studio Code resulting in error

I'm relatively new to coding in C++ and have started working with main and header files and I've created a program to test it out, however, the following program results in the following compiler error:
Undefined symbols for architecture x86_64:
"Print()", referenced from:
_main in test-7d0225.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is my current code.
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main() {
Print();
return 0;
}
test.h
#ifndef TEST_H
#define TEST_H
void Print();
#endif
test1.cpp
#include <iostream>
#include "test.h"
using namespace std;
void Print() {
cout << "Hello" << endl;
}
Nothing I've found online has helped me and my only assumptions are that my compiler isn't set up correctly. I've tried compiling both of these of files by typing in "g++ test.cpp test1.cpp", as well, but yields similar results. I would like to note that I am on Mac as well. Please feel free to leave any comments or suggestions for how I've asked this question, this is my first time on stack overflow.
Your code seems just fine, I’ll will try to compile it on vscode myself.
One problem could be your vscode launch.json. I have had the same problem as what I am describing. Vscode can be quite “Finicky” with low level languages like c and c++. In your launch file (or tasks file if it calls something in tasks) make sure you compile *.cpp files in the folder. I would look something like {folder}/**.cpp. To find the exact command look on vscode’s official site.
Given that you tried compiling your code outside of vscode and assuming that everything is in the same folder, it might be a coding problem. Again, your code looks fine. Go online and find some learning to code website with multiple file coding. Copy their code and do the same thing that you did with your code. If it doesn’t work there is definitely a problem with how to code is being compiled. Otherwise revise your code and compare to find the problem.
Also, could you provide the terminal command vscode prints (I am also on Mac). It will be in the terminal tab in the g++ section, not in bash)
P.S. I am also new to stack overflow so any suggestions you have for me will be great.

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.

SDL Error Undefined symbols for architecture x86_64 "_SDL_main"

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.

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[])

Undefined Symbol: _sf_open (Simple audio stuff on OSX)

I'm trying to get into C++ programming, and I'm quite struggling against the little details. Right now, I'm trying to get the snippet below to work, and apparently it will compile, but not link. (error message is a the bottom of this post)
I'm using libsndfile to open audio files, and the linker doesn't seem to find the corrent library files for it. This is on OS X 10.5.
I've downloaded libsndfile from mega-nerd.com and installed it via the ususal configure, make, sudo make install procedure. Is there anything else I need to do?
Edit: I have macports installed on my system, if that's of any concern.
Here's my code:
#include <iostream>
#include <string>
#include <sndfile.h>
#include "stereoSplit.h"
using namespace std;
int stereoSplit(string filename)
{
cout << filename;
SF_INFO sfinfo;
sfinfo.format = 0;
SNDFILE * soundfile;
soundfile = sf_open( filename.c_str(), SFM_READ, &sfinfo );
return 0;
}
int main (int argc, char const *argv[])
{
return stereoSplit("testStereo.wav");
}
And here's the complete error message:
Undefined symbols:
"_sf_open", referenced from:
stereoSplit(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in cc3hvkkk.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
You need to link in the library. Your compiler command should look something like:
g++ mystuff.cpp -lsndfile
To link the library in Xcode, select the project target, add a new item to Link binary with libraries, and find the libsndfile.1.dynlib on your computer.