Adding and Using Dynamic Libraries with Xcode - 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.

Related

Xcode/include cpp file/ clang: error: linker command failed with exit code 1

when using Xcode to write C++, I have one main.cpp and some other support files(.h, .cpp) which used in main.cpp(#include "file name"), even though there is no error about the include, but it turn out:
**Undefined symbols for architecture arm64:** "Mesh::readOBJFile(char const*)", referenced from: _main in main-b652fe.o "Mesh::Mesh()", referenced from: _main in main-b652fe.o "Mesh::~Mesh()", referenced from: _main in main-b652fe.o **ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)**
After checking almost all the related answers in the stackflow. I found the reason of this error is the setting of Xcode it self.
I check the build Phase : Compile Source. I found there is only main.cpp and no other support files. This is because when I add new files to the project, I choose the "create folder reference" which do not add the files to the compile list and finally cause this issue.
Finally, I add the files again, and chose "Create groups" . and this works. I think the error message here indicate the support files are not correctly compiled, therefore we can check the compile list about this. I hope it helps!

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

imwrite throwing compile error in OpenCV

I have been using OpenCV for a while and also the imwrite function, but unfortunately this is not working any more.
I am running with OpenCV 2.4.3 with following sample code:
imwrite("somepath/somefile.png", myMat);
The error:
Undefined symbols for architecture x86_64:
"cv::imwrite(std::string const&, cv::_InputArray const&, std::__debug::vector<int, std::allocator<int> > const&)", referenced from:
MyProject::this_callback(int, void*) in MyProject.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The error looks somewhat familiar but i cannot determine whats wrong.
Yes, I've thought you were using XCode. I had the same problem. :)
If you change the project setup so that:
you use GNU++11 as C++ language dialect
libstdc++ (GNU C++ standard) as C++ standard library
your linking problem will go away.
I use Apple LLVM 4.1.
When I had this problem, I have tried just adding a new target to one of my old projects I knew, worked. Then I've just made that target a one-source-file program.
This must be a "magic" part of XCode as I think there was a time I could not get the same project working after a restart. :S
I did what Barnabas did and also got the issue on cvdef.h. I was getting desperate so and what I did was just changed the header file.
in /usr/local/cvdef.h line 205:
I changed
include <cstdint>
typedef std::uint32_t uint;
to:
include <tr1/cstdint>
typedef std::tr1::uint32_t uint;
based on this post
I think opencv devs should apply some changes to the code for 64bit..
use something like clang version 3.8.0 (trunk 257459), instead of gcc version 4.9.2 (MacPorts gcc49 4.9.2_1+universal) to compile
for reference, cmd: clang++ -std=c++11 `pkg-config --cflags --libs opencv` code.cpp -o code

Trying to get stasm to work on Ubuntu

I have the below in a Cmake file:
add_library(stasm STATIC IMPORTED)
set_property(TARGET stasm PROPERTY
IMPORTED_LOCATION /media/Data/sdks/stasm3.1/linux/libstasm.a)
target_link_libraries( StasmOpencvExample ${OpenCV_LIBS} stasm)
I generated the libstasm.a by doing:
How to create a static library with g++? , the first answer, taking all of the .o files from the linux folder and putting it into an archive.
but when i run make on my project i get:
Scanning dependencies of target StasmOpencvExample
[100%] Building CXX object CMakeFiles/StasmOpencvExample.dir/stasm_opencv_example.cpp.o
Linking CXX executable StasmOpencvExample
CMakeFiles/StasmOpencvExample.dir/stasm_opencv_example.cpp.o: In function `main':
stasm_opencv_example.cpp:(.text+0x9a): undefined reference to `AsmSearchDll(int*, int*, char const*, char const*, int, int, int, char const*, char const*)'
collect2: ld returned 1 exit status
has anyone gotten a cmake project to work with stasm on linux before? I also had to remove a n include "windows.h" from stasm_dll.cpp, and other windows specific code that wasn't properly done to allow working on linux.
I have already gotten the linux folder to generate the binaries and they work great, now I just need to incorporate this functionality into my own project..
It seems that the reason was that Stasm was created to make executables on Windows that did Image processing. Stasm DOES NOT act like a library, I'm currently making my own static and shared library that will act like a library that you can use to import into a random project and specify the parameters.
Basically modifiying main.cpp to another class and taking out the testing/unecessary code and get a thin processing version.

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