Error when trying to compile in Xcode with SDL - xcode

I am trying to compile a project with SDL in Xcode and get the error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
-u command line option
I have the SDL.framework include along with Cocoa.framework in the Link Binary with Libraries. I also have SDLMain.h and SDLMain.m in the project.
This is all my code:
#include "SDLMain.h"
#include <SDL/SDL.h>
int main(int argc, const char * argv[])
{
return 0;
}

int main has to look like this:
int main(int argc, char * argv[])
{
return 0;
}
get rid of the const

It tool a quite long time until I got SDL and Xcode running. So, don't care. :-)
I uploaded here a simple SDL template for Xcode 4.5 and Mac OS X 10.7 and 10.8 (also using OpenGL 3.2 Core Profile possible). Step by Step instructions:
Download SDL (at the moment version 1.2.15)
Open the downloaded .dmg file
copy the SDL.framework into /Library/Frameworks/
Done. You can use the Xcode template (you should see a red area):
Further details and an image on my Blog (only german, sorry).

Related

Adding and Using Dynamic Libraries with Xcode

Ok. So here goes. I am currently trying to add and use a dynamic library called OpenCV into a Xcode project of mine. I first installed MacPorts using the .pkg. Then I opened Terminal and ran "sudo port install opencv". MacPorts then installed all sorts of dependencies (FFMPEG/etc) and then installed OpenCV into the "/opt/local/lib" folder for the .dylib files and into "/opt/local/include" folder for the header files. Then I opened Xcode and opened command line utility application project. I added a group to my project and then added all the .dylib files inside of it (I think these are called references to the dynamic libraries, but I am not sure - I am a total beginner - I learned C++, my first programming language, merely a week ago"). After that, I clicked on my project file thing inside of Xcode and went to Build Settings and then changed the "Header Search Paths" under the "Search Paths" tab to the /include folder I mentioned previously. The Library Search Path was already set to the /lib folder since I added the .dylib files already (I think that's what happened - not sure). Anyways, then I copied and pasted the following sample code from the opencv website into my main.cpp file just to test whether or not my library works.
//******************************Sample Code ************************************************
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
if( argc != 2) {
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1; }
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
if(! image.data ) {
// Read the file
// Check for invalid input
cout << "Could not open or find the image" << std::endl ;
return -1; }
namedWindow( "Display window", CV_WINDOW_NORMAL );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0; }
//********************END OF SAMPLE FILE CODE **********************************************
After I did all that and before I hit run, I think it is important to note that Xcode did not complain that it couldn't find my header files, and also it didn't complain about any syntax errors with the new functions the library should've added. In short, there were no errors highlighted on the main.cpp file. All the errors, however, started showing up after I hit the Run button inside of Xcode. Here are all the errors that it generated:
**************Errors from Xcode after I hit Run*********************************************
Stupid Stack Overflow won't let me post images, so here is the link to the error photo:
*****************END OF BLOCK***************************************************************
So, yeah, I have no clue what I am doing at this point and am totally lost. Did I not follow the correct steps in adding a dynamic library to my project? Oh, and I forgot what it was, but I somehow got Xcode to ignore those semantic issues (I cannot remember how I did this or if that was what I did), but once the semantic issues are ignore, the only error that remains seems to be something related to a "Mach-O Linker Error" which says something on the lines of: "undefined symbols....architecture/etc" and then says that the undefined symbol was one of the library functions that I was calling in my main.cpp file. But that error no longer occurs since I deleted everything and started again. So now the only errors I get are the ones from the picture. So PLEASE HELP!!!! I am absolutely lost and I have been researching for weeks now, but I think the issue with me there is that I have no clue what I am looking for or what question I should be asking. The steps I listed above is only what I have done. Any insight will be much appreciated. THANKS IN ADVANCE!!!!!!
____________EDIT___________________
So I repeated the above steps again, but this time I get this error:
Ld /Users/Yash/Library/Developer/Xcode/DerivedData/OpenCVOptTEST-adchdkiefzmijfhahwtzcfqarzkw/Build/Products/Debug/OpenCVOptTEST normal x86_64
cd "/Users/Yash/Coding Folder/OpenCVOptTEST"
setenv MACOSX_DEPLOYMENT_TARGET 10.8
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/Yash/Library/Developer/Xcode/DerivedData/OpenCVOptTEST-adchdkiefzmijfhahwtzcfqarzkw/Build/Products/Debug -L/opt/local/lib -F/Users/Yash/Library/Developer/Xcode/DerivedData/OpenCVOptTEST-adchdkiefzmijfhahwtzcfqarzkw/Build/Products/Debug -filelist /Users/Yash/Library/Developer/Xcode/DerivedData/OpenCVOptTEST-adchdkiefzmijfhahwtzcfqarzkw/Build/Intermediates/OpenCVOptTEST.build/Debug/OpenCVOptTEST.build/Objects-normal/x86_64/OpenCVOptTEST.LinkFileList -mmacosx-version-min=10.8 -stdlib=libc++ -lopencv_calib3d.2.4.6 -lopencv_contrib.2.4.6 -lopencv_core.2.4.6 -lopencv_features2d.2.4.6 -lopencv_flann.2.4.6 -lopencv_gpu.2.4.6 -lopencv_highgui.2.4.6 -lopencv_imgproc.2.4.6 -lopencv_legacy.2.4.6 -lopencv_ml.2.4.6 -lopencv_nonfree.2.4.6 -lopencv_objdetect.2.4.6 -lopencv_photo.2.4.6 -lopencv_stitching.2.4.6 -lopencv_superres.2.4.6 -lopencv_ts.2.4.6 -lopencv_video.2.4.6 -lopencv_videostab.2.4.6 -o /Users/Yash/Library/Developer/Xcode/DerivedData/OpenCVOptTEST-adchdkiefzmijfhahwtzcfqarzkw/Build/Products/Debug/OpenCVOptTEST
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::__1::basic_string, std::__1::allocator > const&, int)", referenced from:
_main in main.o
"cv::imread(std::__1::basic_string, std::__1::allocator > const&, int)", referenced from:
_main in main.o
"cv::imshow(std::__1::basic_string, std::__1::allocator > const&, cv::_InputArray const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Check out Apple's documentation for working with Dynamic Libraries.
What these errors are telling you is that it can't find the object code (implementations) for these symbols during the Linking phase. From Apple's documentation, it looks like it expects to find the dylibs in /usr/local/lib not /opt/local/lib.

How to build qt 5 project on mac

I try to build a simple Qt 5 program on Mac. But I failed.
The code is very simple:
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication app (argc, argv);
return app.exec();
}
I used:
clang++ -I ~/Qt5.0.0/5.0.0/clang_64/include -L/Users/crazylion/Qt5.0.0/5.0.0/clang_64/lib test.cpp
Then I got this error:
Undefined symbols for architecture x86_64:
"QApplication::exec()", referenced from:
_main in test-jPGORy.o
"QApplication::QApplication(int&, char**, int)", referenced from:
_main in test-jPGORy.o
"QApplication::~QApplication()", referenced from:
_main in test-jPGORy.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is there anything i missing?
Firstly, don't compile and link Qt projects by hand; use qmake and project files.
Run qmake -project in your source directory to generate a basic project file.
Edit the project file and add the following line: QT += widgets
Now run qmake to generate a makefile.
Now run make to build your program.
Secondly, you can simply #include <QApplication>
If you want to use clang++ in favor of qmake, you need to specify libraries to link against to, along with the library directory (which you supplied).
clang++ -I ~/Qt5.0.0/5.0.0/clang_64/include -L/Users/crazylion/Qt5.0.0/5.0.0/clang_64/lib -lQtCore -lQtGui test.cpp
I had the same problems and it seems to me that there is some sort of bug in the release it gave me some errors because out of a fresh install (using qt creator) i didn't have some obscure qt library (not the normal qt5 modules but some sort of library in development) so I tend to think that it could be qt's problem
That said I have some questions to better understand:
-are you using a IDE?
-if you are using one which is it?
-are you including all the modules in the *.pro for quake?
-have you used 4.8 version, did you experienced these problems with that?
P.S. if you do not have any particular necessity I suggest you to stick to version 4.8 for some time (as I am doing without problem) since 5.0 is just been released

Allegro Framework Cannot Be Found in Xcode [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Xcode 4 and Allegro 5.1 - Linker can't find Framework
I've been trying to get Allegro working on my Mac running OS X 10.8. I followed these instructions:
http://wiki.allegro.cc/index.php?title=OSX,_Xcode_4,_Framework
I go down and do everything they say for a simple Xcode application but it fails during the build with the following error:
ld: framework not found Allegro-5.1
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've tried using Allegro 5.0.7 and 5.1.3 and both times I get the same result that it can't find the framework. Am I missing something? Here's my very basic app that I'm trying to run.
#include <allegro5/allegro.h>
int main(int argc, char **argv) {
al_init();
al_create_display(640, 480);
al_clear_to_color(al_map_rgb_f(1, 1, 0));
al_flip_display();
al_rest(5.0);
return 0;
}
The answer for "Xcode 4 and Allegro 5.1 - Linker can't find Framework" helped me.
In the project target, go to "Build Settings" and change the "Framework Search Paths" to /Library/Frameworks.

Why can't I compile this command-line OpenCV Mac application?

Following is my step:
1)create a command line tool project "OpenCV"
2)add files to the project which are in /usr/local/lib with suffix 2.4.2, such as
"libopencv_calib3d.2.4.2.dylib"
3)add "/usr/local/include" to project's Header Search Path
4)type this program:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.hpp>
int main(int argc, char** argv)
{
IplImage * pInpImg = 0;
// Load an image from file - change this based on your image name
pInpImg = cvLoadImage("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!pInpImg)
{
fprintf(stderr, "failed to load input image\n");
return -1;
}
// Write the image to a file with a different name,
// using a different image format -- .png instead of .jpg
if( !cvSaveImage("my_image_copy.png", pInpImg) )
{
fprintf(stderr, "failed to write image file\n");
}
// Remember to free image memory after using it!
cvReleaseImage(&pInpImg);
return 0;
}
However, I get error :
ld: library not found for -lopencv_calib3d.2.4.2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Where is the problem?
I'm using mountain lion and Xcode 4.4
You don't need to add the opencv libs to your project but you do need to link to the libraries and set the library search path. I was able to compile and run your program with these settings:
Search paths:
Link to libraries:

Setting up SDL on Mac OS X Lion with Xcode 4

I've been trying to get a super simple SDL program to work. I'm using Mac OS X Lion. I've got SDL to work in Snow Leopard, but it doesn't seem to want to work in lion. So far I have this:
#include <iostream>
#include "SDL/SDL.h"
using namespace std;
/*
#ifdef main
# undef main
#endif
*/
int main( int argc, char* args[] )
{
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
hello = SDL_LoadBMP( "hello.bmp" );
SDL_BlitSurface( hello, NULL, screen, NULL );
SDL_Flip( screen );
SDL_Delay( 2000 );
SDL_FreeSurface( hello );
SDL_Quit();
return 0;
}
When I try to compile this code (in Xcode 4.1) it gives me this error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: _SDL_main)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I uncomment the #ifdef stuff I have commented currently, the program compiles, but then receives SIGABRT on the SDL_SetVideoMode line. That commented stuff I have I just saw in another program, I'm not sure if I'm supposed to have it or not.
How am I supposed to get this working?
The SDL header files redefine main with a macro. This is in SDL_main.h:
#define main SDL_main
But this is fine. SDL provides its own main() function that then calls your version. So get rid of those defines, they are making it worse, not better.
If your project is Cocoa based, then you probably missed to include SDLmain.m in your project. This provides a Cocoa friendly main() function. If your project is native C++, then my guess is that you did not include all the SDL libs in your project, so the linker isn't seeing SDL's own main().
If you are using the SDL framework you simply need to add the files SDLMain.h and SDLMain.m to your project (assuming you already have added SDL.framework to your project).
You find these files in the "devel-lite" folder of the SDL diskimage, which you can download here: http://www.libsdl.org/release/SDL-1.2.15.dmg
These two files will give you a Cocoa-friendly main routine, so that your SDL application can be a well-behaving OS X application.
SDL.h does not include SDL_main.h
The first line in your file should be:
#include SDL_main.h
SDL_main redefines the main function and then does its own initialization the is required to get SDL working with OS X
When compiling, you will also need to link in libSDLmain in addition to libSDL
I had the same link time error as OP for a pure C++ & OpenGL app, and the solutions was to use the sample project from https://github.com/Ricket/HelloSDL
This made me add the Cocoa libraries but that would have been needed anyway since I was targeting the iPhone.

Resources