Linker Error when building Xcode project using external library - c++

I am attempting to build a very simple command line application, in Xcode, that will print out basic information about MXF video files. In order to do this I need to use the libmxf, libbmx, and libbmx libraries available for download here:
http://sourceforge.net/p/bmxlib/home/Home/
My C++ code is incredibly simple at this point:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <vector>
#include <bmx/mxf_reader/MXFFileReader.h>
#include <bmx/mxf_reader/MXFGroupReader.h>
#include <bmx/mxf_reader/MXFSequenceReader.h>
#include <bmx/mxf_reader/MXFFrameMetadata.h>
#include <bmx/MXFUtils.h>
#include <bmx/Utils.h>
using namespace std;
using namespace bmx;
#define MXF_OPEN_READ(fn, pf) mxf_disk_file_open_read(fn, pf)
int main(int argc, const char * argv[])
{
std::vector<const char *> filenames;
std::cout << "mxfheader: execution beginning...\n";
for (int cmdln_index = 0; cmdln_index < argc; cmdln_index++) {
if (!check_file_exists(argv[cmdln_index])) {
if (argv[cmdln_index][0] == '-') {
fprintf(stderr, "Unknown argument '%s'\n", argv[cmdln_index]);
} else {
fprintf(stderr, "Failed to open input filename '%s'\n", argv[cmdln_index]);
}
return 1;
}
filenames.push_back(argv[cmdln_index]);
}
std::cout << filenames[0] << "\n";
return 0;
}
When I compiled the BMX library, I was sure to run configure with 64-bit support, like so:
./configure --build=x86_64-apple-darwin11.4.2 --host=x86_64-apple-darwin11.4.2 CFLAGS="-arch x86_64" CXXFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" CC=clang CXX=clang++
In the XCode Project under Build Settings, I have added /usr/local/lib to my Search Paths. Under Build Phases, I have added "libbmx-0.1.3.dylib", "libMXF-1.0.4.dylib", and "libMXF++-1.0.4.dylib" to the "Link Binary With Libraries" section.
I have verified that these libraries are, indeed, 64-bit ( file libbmx-0.1.3.dylib returns libbmx-0.1.3.dylib: Mach-O 64-bit dynamically linked shared library x86_64 ).
Every time I try and build the application, I get the following linker error:
Ld /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader normal x86_64
cd /Users/ned/Documents/src/mxfheader
setenv MACOSX_DEPLOYMENT_TARGET 10.7
/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/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -L/usr/local/lib -F/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -filelist /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Intermediates/mxfheader.build/Debug/mxfheader.build/Objects-normal/x86_64/mxfheader.LinkFileList -mmacosx-version-min=10.7 -stdlib=libc++ -lbmx-0.1.3 -lMXF-1.0.4 -lMXF++-1.0.4 -o /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader
Undefined symbols for architecture x86_64:
"bmx::check_file_exists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", 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)
Any help would be appreciated. Thanks!

Your problem is the option: -stdlib=libc++ in the command line. It's causing a link to the wrong libc++, you need to make it -stdlib=libstdc++, as this is the stdlib that the libbmx library is compiled against.
under the Apple LLVM compiler options for the C++ standard library, select: libstdc++, or pick compiler default (which should choose libstdc++ also)

Related

Using armadillo in atom with script

I there a way to compile a c++ file using script in atom that uses armadillo? I couldn't find anything related to it.
I already installed armadillo and tried compiling some basic code using script in atom:
#include <iostream>
#include <armadillo>
int main(int argc, const char **argv) {
arma::arma_rng::set_seed_random();
arma::Mat<double> A = arma::randu(4,4);
std::cout << "A:\n" << A << "\n";
return 0;
}
This is the error I got:
Undefined symbols for architecture x86_64:
"thread-local wrapper routine for arma::arma_rng_cxx11_instance", referenced from:
arma::arma_rng::set_seed(unsigned long long) in test1-83e853.o
arma::arma_rng::randu<double>::fill(double*, unsigned long long) in test1-83e853.o
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 read the source code for this atom-script thing correctly, it just hardcodes -Wall -include stdio.h as compilation options. You need to add -larmadillo to link it to the Armadillo library.
In short: you cannot use atom-script in combination with custom libraries, unless you edit the extension to hardcode the libraries in your compilation commandline.

Setting up GMP Library on XCode for C++

I had a question on setting up GMP Library to work on XCode for C++ programming. Although I have tried pursuing the GMP manual and also other similar stack questions on getting the library to work properly, I still have not been able to properly get the library to work. I am new to C++ and programming in general so please bear with me.
Things I have tried:
Install GMP library through homebrew using brew install gmp
Using the XCode interface under header search paths and library search paths added the path "/usr/local/include" (this is where homebrew installs the gmp library)
Under other linker flags added -lgmp -lgmpxx
Tried running various sample codes e.g.
#include <iostream>
#include <stdio.h>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main ()
{
mpz_t p;
mpz_init(p);
return 0;
}
However this code fails to compile and the XCode debugger has the following error messages:
Undefined symbols for architecture x86_64:
"___gmpz_init", 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)
Thanks!

ld: symbol(s) not found for architecture

First of all I want to say I realise this has been asked a dozen times, and I've read countless solutions without any luck so therefore I'm asking again.
I'm using OS X Mavericks 10.9.5, writing in Sublime 3 and compiling using terminal with g++
I'm trying to compile this simple file (test.cpp)
#include <iostream>
#include <SDL2/SDL.h>
int main () {
if(SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "I made it this far" << std::endl;
}
return 0;
}
Compiler line :
g++ test.cpp
This returns the error :
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-ebbae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, I've tried adding a lot of different flags, -m32 only changes the result to throw Undefined symbols for architecture i386: and ld: symbol(s) not found for architecture i386
I've tried different -arch variations, but I can't seem to get it to work. also played around with -l -I flags but I'm not sure I know what they do/how they could help..
Does anyone have a clue what to do next?
EDIT : Some additional information. I've tried using the .framework for SDL2, that didn't change anything, currently I'm using SDL2 installed through compilation of the source. Headers located in usr/local/SDL2
g++ test.cpp
You should specify the SDL library, too:
g++ test.cpp -lsdl2
You might need to include a path if its not well known to the compiler driver:
g++ test.cpp -L/path/to/the/library -lsdl2

GDAL API in C/C++

I'm trying to compile the following program (using GDAL API in C++):
main.cpp:
#include "gdal_priv.h"
#include "cpl_conv.h"
int main(int argc, char *argv[])
{
GDALAllRegister();
GDALDataset* data = (GDALDataset*) GDALOpen( "heightmap.tiff", GA_ReadOnly );
}
the way I build it on a Mac :
clang main.cpp -I/Library/Frameworks/GDAL.framework/Versions/1.10/unix/include
and got this:
clang main.cpp -I/Library/Frameworks/GDAL.framework/Versions/1.10/unix/include/
Undefined symbols for architecture x86_64:
"_GDALOpenShared", referenced from:
_main in test_gdal-QIB6MK.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What's wrong with this very basic case?
Try to build your project on a Mac using bash file:
#!/usr/bin/env bash
## Path to you installed GDAL
GDAL_PATH=/usr/local/Cellar/gdal2/2.2.0
INCLUDE_PATH=${GDAL_PATH}/include
LIBS_PATH=${GDAL_PATH}/lib
GDAL_LIB=-lgdal
g++ -I${INCLUDE_PATH} -L${LIBS_PATH} ${GDAL_LIB} main.cpp -o main

xcode c++ sqlite3 symbol(s) not found for architecture x86_64

Hi I want to use sqlite in c++ project in xcode 4
now i am getting this error
Ld /Users/jayb/Library/Developer/Xcode/DerivedData/EMS-bpigynlzjbrescadebhoiupqmtkg/Build/Products/Debug/EMS normal x86_64
cd /Users/jayb/Documents/Developement/EMS/EMS
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/jayb/Library/Developer/Xcode/DerivedData/EMS-bpigynlzjbrescadebhoiupqmtkg/Build/Products/Debug -F/Users/jayb/Library/Developer/Xcode/DerivedData/EMS-bpigynlzjbrescadebhoiupqmtkg/Build/Products/Debug -filelist /Users/jayb/Library/Developer/Xcode/DerivedData/EMS-bpigynlzjbrescadebhoiupqmtkg/Build/Intermediates/EMS.build/Debug/EMS.build/Objects-normal/x86_64/EMS.LinkFileList -mmacosx-version-min=10.8 -o /Users/jayb/Library/Developer/Xcode/DerivedData/EMS-bpigynlzjbrescadebhoiupqmtkg/Build/Products/Debug/EMS
Undefined symbols for architecture x86_64:
"_sqlite3_close", referenced from:
_main in main.o
"_sqlite3_errmsg", referenced from:
_main in main.o
"_sqlite3_open", 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)
and i found that is linker problem
it may fix with compiler option -lsqlite3
but, how can i add that option in Xcode??????
I use "Run" button on xcode 4.4 to compile my project.
i am not compiling in terminal window.
this is my code
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>
using namespace std;
int main()
{
sqlite3 *db;
int rc = sqlite3_open("EMSDB", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}
else {
fprintf(stderr, "Opened Database successfully\n");
}
sqlite3_close(db);
return 0;
}
I found the way,
wish it helps someone looking for the same solution.
now it builds correct and i can see the output
woops, I cannot post image yet :(
from the xcode build settings,
you can find a tab called (Linking)
and on the Linking tab
there are field call 'Other Linker Flags'
i simply added the -lsqlite3 for both Debug, and Release
cheers
I was getting the similar error : I did following in my case:
#import <sqlite3.h>
I am not familiar with xcode. Look for linker settings or compiler settings and add the -lsqlite3 there.
Perhaps this page helps: Xcode what's the difference between "Other Linker Flags" vs "Other_LDFLAGS"
LDFLAGS are passed to the linker. CFLAGS are passed to the compiler.
It is require to link libsqlite3.dylib in your project. That can be done in
Linked Frameworks and Libraries and add libsqlite3.dylib.
for reference follow:
xcode sqlite3 libsqlite.dylib