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.
Related
I'm building a Unity project for an iOS8 simulator. Moving this for Xcode 6 GM for the simulator has resulted in this linker error. Not sure if I'm missing something in the build or something is broken. Any suggestions? The full error is:
Undefined symbols for architecture i386:
"_clock$UNIX2003", referenced from:
_substanceHandleSwitchHard in libiPhone-lib.a(apihandle.o)
_mainRenderProcess in libiPhone-lib.a(mainrenderprocess.o)
ld: symbol(s) not found for architecture i386
Exit with code 1
Add the following at the end of main.mm.
#include <time.h>
extern "C"
{
clock_t
clock$UNIX2003(void)
{
return clock();
}
}
clock$UNIX2003 is a symbol that is provided by OS X and is not part of the iOS Simulator runtime. iOS is always conformant and thus does not have legacy (non $UNIX2003) variants of functions (which are provided for binary compatibility with code built against older versions of the OS X SDK).
The common cause of the issue you are seeing is that you have an object file or archive (libsomething.a) that was built against the OS X SDK and are trying to link it into your iOS Simulator executable. That is not supported as the two platforms are not binary compatible at that layer.
You need to rebuild your library (the libsomething.a) against the iOS Simulator SDK.
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.
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
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).
I have a PS3 that I've installed YDL 6.1 and SDK 3.1 on and everything seems to be working fine, as I can compile and run the examples. However, I've run into some problems with writing programs of my own. I've created a small test case that seems to pinpoint the cause of the failure. I have the following code:
// mathtest.c
#include <stdio.h>
#include <math.h>
int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%lf) = %lf\n", param, result );
return 0;
}
When I then run
ppu-gcc mathtest.c
I get the following error
/tmp/ccFqwJdG.o:(.text+0x20): undefined reference to `sqrt'
collect2: ld returned 1 exit status
I already checked to make sure that math.h exists on this system and it does define sqrt. I also already tried running this:
ppu-gcc -I/usr/includes/ mathtest.c
but it results in the same error. I'm confused, anyone have any ideas?
I sometimes got similar errors on Linux, using -lm as a gcc parameter helped there. Perhaps it does here, too. The parameter tells the linker to include the math library, too.