clang++ mac os x c++11 linker issue - c++11

I have a problem compiling a program with "-std=c++11 -stdlib=libc++" under mac os x 10.8.3 using clang++ from xcode 4.6.2.
When I try to use std::mem_fn() or (deprecated) std::mem_fun_ref(), I get linker error "symbol(s) not found". The same code (with std::mem_fun_ref instead of std::mem_fn) compiles and links without any issues under the c++03 standard.
If I call the same member function on an object without referring to it via mem_fn or mem_fun_ref, the program compiles and runs without any problems. Is it a clang++ problem, a mac os problem, or am I doing something wrong?
The code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char **arvc)
{
/* The beginning of the function compiles without any errors */
string str = "It's a test)";
if (str.empty())
{
cout << "String is empty!!!" << endl;
}
vector<string> v1;
v1.push_back("str1");
v1.push_back("str2");
v1.push_back("");
v1.push_back("str4");
v1.push_back("");
v1.push_back("str6");
/* The code after this point leads to linker error */
vector<string>::iterator it = remove_if(v1.begin(), v1.end(), mem_fn(&string::empty));
v1.erase(it, v1.end());
for (it = v1.begin(); it < v1.end(); ++it)
{
cout << " '" << *it << "'" << endl;
}
return 0;
}
The compilation log:
$ xcrun clang++ -v -std=c++11 -stdlib=libc++ ../src/test_cxx.cpp
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test_cxx.cpp -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 136 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.2 -isysroot / -fmodule-cache-path /var/folders/nz/0x05d6p14gddzxxn8ymnn74c0000gn/T/clang-module-cache -stdlib=libc++ -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Volumes/Work/projects/web/sp3/build -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.8.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/nz/0x05d6p14gddzxxn8ymnn74c0000gn/T/test_cxx-kAQ3wk.o -x c++ ../src/test_cxx.cpp
clang -cc1 version 4.2 based upon LLVM 3.2svn default target x86_64-apple-darwin12.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.2/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -syslibroot / -o a.out /var/folders/nz/0x05d6p14gddzxxn8ymnn74c0000gn/T/test_cxx-kAQ3wk.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
_main in test_cxx-kAQ3wk.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 a bug in clang. I can't find a bug report on it though. A bug report would be much appreciated.
The problem is that string::empty is marked "always_inline", but also the string template is marked extern, meaning that it has been instantiated already for you.
For some reason when we have this combination, and you form a member function pointer to the always_inline member, clang refuses to outline it so that it can point to it. It only refuses if the instantiation is marked as extern.
If you disable either the extern, or the always_inline, then the code will work. You can disable the former by including this in "C++ Other Flags":
-D'_LIBCPP_EXTERN_TEMPLATE(...)='

Related

OpenMP error on MacOS - ld: symbol(s) not found for architecture x86_64

I have installed OpenMP and xcode-select in my macos.
$ clang-omp --version
clang version 3.5.0
Target: x86_64-apple-darwin15.4.0
Thread model: posix
$ xcode-select --version
xcode-select version 2343.
While running following pretty simple code
1 #include <omp.h>
2 #include <stdio.h>
3
4 #define n 20
5
6 int main(){
7
8 int i;
9 int tid;
10
11 omp_set_num_threads(3);
12 #pragma omp parallel for private(tid) schedule(static,1)
13
14 for (i=0; i<n; i++) {
15 tid = omp_get_thread_num();
16 printf("Thread %d executing iteration %d\n", tid, i);
17 }
18 }
I get error
$ clang-omp -openmp parallel.c -v
clang version 3.5.0
Target: x86_64-apple-darwin15.4.0
Thread model: posix
"/usr/local/Cellar/clang-omp/2015-04-01/libexec/bin/clang-3.5" -cc1 -triple x86_64-apple-macosx10.11.0 -emit-obj -mrelax-all -disable-free -main-file-name parallel.c -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -v -dwarf-column-info -resource-dir /usr/local/Cellar/clang-omp/2015-04-01/libexec/bin/../lib/clang/3.5.0 -fdebug-compilation-dir /Users/prayagupd/prayag.data/workspace.programming/parallel-programming/openMP -ferror-limit 19 -fmessage-length 178 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/63/jvvb4wy16gx6w76mkgsvn52m0000gn/T/parallel-32b98e.o -x c parallel.c
clang -cc1 version 3.5.0 based upon LLVM 3.5.0svn default target x86_64-apple-darwin15.4.0
ignoring nonexistent directory "/usr/local/Cellar/clang-omp/2015-04-01/libexec/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/local/Cellar/clang-omp/2015-04-01/libexec/bin/../lib/clang/3.5.0/include
/usr/local/opt/libiomp/include/libiomp
/usr/local/Cellar/clang-omp/2015-04-01/libexec/include/c++/v1
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -dynamic -arch x86_64 -macosx_version_min 10.11.0 -o penmp /var/folders/63/jvvb4wy16gx6w76mkgsvn52m0000gn/T/parallel-32b98e.o -lSystem
Undefined symbols for architecture x86_64:
"_omp_get_thread_num", referenced from:
_main in parallel-32b98e.o
"_omp_set_num_threads", referenced from:
_main in parallel-32b98e.o
ld: symbol(s) not found for architecture x86_64
clang-3.5: error: linker command failed with exit code 1 (use -v to see invocation)
gcc doesn't even find omp.h.
$ gcc parallel.c -fopenmp -o parallel
parallel.c:1:10: fatal error: 'omp.h' file not found
#include <omp.h>
^
1 error generated.
gcc version is
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
For your clang-omp issue, did you export the library path? ala
export DYLD_LIBRARY_PATH=(OMPLIB PATH):$DYLD_LIBRARY_PATH
Maybe you didn't install the OpenMP library from Intel? Modern clang(>3.7), that isn't packaged with OS X, now supports OpenMP out of the box.
For your gcc issue, OS X doesn't actually provide gcc, it just links it to vanilla clang. So you are trying to compile it with a compiler which doesn't support OpenMP. If you want gcc, then install it via ports or homebrew.

How to make macbook pro recognize GLEW and GLFW?

I am new to macbook and apple computers in general, so I'm having a hard time making macbook pro recognize GLEW and GLFW. I installed both libraries with no problem, my includes are as follows: /usr/include/GL for glew.h, glxew.h and wglew.h. /usr/lib/ contains libGLEW.a the include and lib files for GLFW are in /usr/local/ I am using vim as a text editor and have a simple main.cpp program but it gives me an error that the glew.h cannot be found, any ideas why this is happening and how should I fix it?
#include <GL/glew.h>
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <stdio.h>
int main()
{
if (!glfwInit())
{
fprintf (stderr, "ERROR: could not start GLFW\n");
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Shaders", NULL, NULL);
if (!window)
{ fprintf(stderr, "ERROR: could not openwindow with GLFW3\n");
glgwTerminate();
return 1;
}
glfwMakeContextCurrent (window);
glewExperimantal = GL_TRUE;
glewInit();
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);
glEnable (DEPTH_TEST);
glDepthFunc (GL_LESS);
glfwTerminate();
return 0;
}
I am compiling with:
g++ -framework Cocoa -framework OpenGL -framework IOKit -o demo main.cpp -I include -I/sw/include -I/usr/local/include /usr/lib/libGLEW.a /usr/local/lib/libglfw3.a
Edit: moving the GL includes to /usr/local fixed compilation errors but now I get a linker error, any idea what can be done here?
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 241.9 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -I include -I /sw/include -I /usr/local/include -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/yuri13/Documents/shaders -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/b4/j5zrzg_n7hb205xs22z5f4qc0000gp/T/main-ea70be.o -x c++ main.cpp
clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin14.0.0
ignoring nonexistent directory "include"
ignoring nonexistent directory "/sw/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -o demo -framework Cocoa -framework OpenGL -framework IOKit /var/folders/b4/j5zrzg_n7hb205xs22z5f4qc0000gp/T/main-ea70be.o /usr/lib/libGLEW.a /usr/local/lib/libglfw3.a -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_CVDisplayLinkCreateWithCGDisplay", referenced from:
__glfwPlatformGetVideoModes in libglfw3.a(cocoa_monitor.m.o)
__glfwPlatformGetVideoMode in libglfw3.a(cocoa_monitor.m.o)
"_CVDisplayLinkGetNominalOutputVideoRefreshPeriod", referenced from:
_vidmodeFromCGDisplayMode in libglfw3.a(cocoa_monitor.m.o)
"_CVDisplayLinkRelease", referenced from:
__glfwPlatformGetVideoModes in libglfw3.a(cocoa_monitor.m.o)
__glfwPlatformGetVideoMode in libglfw3.a(cocoa_monitor.m.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 you get an error related to including the header file, the compiler cannot find the header. You can pass the -v flag to GCC (incl. g++) or Clang (incl. clang++) to see information about where it’s looking for it.
As this forum post explains, if you receive these link errors:
Undefined symbols for architecture x86_64:
"_CVDisplayLinkCreateWithCGDisplay", referenced from:
__glfwPlatformGetVideoModes in libglfw3.a(cocoa_monitor.m.o)
__glfwPlatformGetVideoMode in libglfw3.a(cocoa_monitor.m.o)
"_CVDisplayLinkGetNominalOutputVideoRefreshPeriod", referenced from:
_vidmodeFromCGDisplayMode in libglfw3.a(cocoa_monitor.m.o)
"_CVDisplayLinkRelease", referenced from:
__glfwPlatformGetVideoModes in libglfw3.a(cocoa_monitor.m.o)
__glfwPlatformGetVideoMode in libglfw3.a(cocoa_monitor.m.o)
ld: symbol(s) not found for architecture x86_64
Then you probably need to link with the CoreVideo framework. If you’re compiling on the command line, you can use -framework CoreVideo.

Why can’t Clang find the library I specified on the command line?

I’m writing some unit tests for a dynamic library on OS X. The dynamic library is in ../MathDll. The library itself is built via the following Makefile:
all: math.dylib
math.dylib: MathDll.cpp MathDll.h
clang -dynamiclib MathDll.cpp -o math.dylib
clean:
rm math.dylib
And running file math.dylib gives
math.dylib: Mach-O 64-bit dynamically linked shared library x86_64
I have a simple program, test1.cpp, that will call one of the functions from this library. I tried to compile it with
clang -I../MathDll -L../MathDll -lmath test1.cpp
but this gives me
ld: library not found for -lmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When I invoke Clang with -v, the output is
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test1.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 224.1 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0 -I ../MathDll -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir "/Users/bdesham/Projects/New GUI/math_library/osx_test" -ferror-limit 19 -fmessage-length 118 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/gc/4w1rdgzx3zz2dbxf0m_yd67c0000gn/T/test1-sL5rwc.o -x c++ test1.cpp
clang -cc1 version 5.0 based upon LLVM 3.3svn default target x86_64-apple-darwin13.0.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
../MathDll
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o a.out -L../MathDll -lmath /var/folders/gc/4w1rdgzx3zz2dbxf0m_yd67c0000gn/T/test1-sL5rwc.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -lmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I must be doing something silly here. What do I need to do to make the linker find my library?
Your library needs to be called libmath.dylib, not just math.dylib.

Qt5.1/Qt5.2 + Mac OS 10.9 (Mavericks) + XCode 5.0.2, Undefined symbols for architecture x86_64

Environment : Mac OS 10.9 + Qt5.1/Qt5.2 + OpenCV2.4.7 + XCode(5.0.2)
I can compile the following program via terminal
g++ -L/usr/local/lib -lopencv_core -lopencv_highgui \
-I/usr/local/include main.cpp
The program a.out runs normally.
However, when using Qt 5.1/5.2 to run this OpenCV program,
I got "Undefined symbols for architecture x86_64".
However, Qt5 works normally for a simple HelloWorld c++ program.
What is going on ?
Here is the code.
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace std ;
using namespace cv ;
int main()
{
Mat img ;
img = imread("image.jpg") ;
return 0;
}
And this is the project setting
INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib
LIBS += -lopencv_core -lopencv_highgui -v
cache()
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
Here is the compile message
/Users/XXX/Qt5.2.0/5.2.0-beta1/clang_64/bin/qmake -spec macx-clang CONFIG+=debug -o Makefile /Users/XXX/Desktop/untitled/untitled.pro
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -Wl,-syslibroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.6 -o untitled main.o -L/usr/local/lib -lopencv_core -lopencv_highgui -v
with -v to show invocation
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -headerpad_max_install_names -macosx_version_min 10.6.0 -o untitled -lcrt1.10.6.o -L/usr/local/lib -syslibroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk main.o -lopencv_core -lopencv_highgui -lstdc++ -lSystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a
And here is the error message
Undefined symbols for architecture x86_64:
"cv::imread(std::string const&, int)", referenced from:
_main in main.o
.... and so on
I checked the problem has nothing to do with -lstdc++ or -std=c++11 or -stdlib=libstdc++.
by using command line compile.
Any ideas or suggestion?
This issue is answered in this post
http://qt-project.org/forums/viewthread/35646/
and explained by sandy.martel
I quoted here
Qt binary distribution compile with -stdlib=libstdc++ to be compatible with 10.6, Xcode 5 on 10.9 will select -stdlib=libc++ by default (for OS X 10.7 and better only).
So symbol using classes from the standard library (like std::string in this case) will not resolve correctly at link time.
This is why you’re seeing this error (Undefined symbols for architecture x86_64) . Look with which standard library opencv is built:
otool -L libopencv_XXX.dylib.
You’ll have to rebuild it with the right one or change Qt’s mkspec to use the newer one.
I solve this by changing ../Qt5.2.0/5.2.0-rc1/clang_64/mkspecs/macx-clang/qmake.conf
from
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6
to
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
And remember clean your project before rebuild.
In case you came here, because you changed config+=11 and nothing happend, try to clean your project before rebuild.
The problem is not with the system. On Mac you just have to include imgcodecs header and include the respective lib files also. This will solve all the trouble you had.
-lopencv_imgcodecs

Link external library GCC OS X

I have installed GCC 4.7.2 and GMP 5.1.0 and I've written this simple code in main.cpp locate at ~/Desktop:
#include <iostream>
#include <gmp.h>
using namespace std;
int main ()
{
mpz_t a;
mpz_init(a);
mpz_set_ui(a, 42);
cout << "Hello, world!" << endl;
}
I compile it with:
$ g++ main.cpp -o exe
but I get this error message:
Undefined symbols for architecture x86_64:
"___gmpz_init", referenced from:
_main in ccC0FXun.o
"___gmpz_set_ui", referenced from:
_main in ccC0FXun.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I think it's because it doesn't find the GMP library, am I right?
So how can I link an external library such as GMP in GCC?
Your program works fine for me here using:
g++ main.cpp -o exe -lgmp
Check the GCC documentation for a description of the -l flag.

Categories

Resources