Trouble compiling c++ program on Mac OSX 10.9 with opencv - c++

I'm trying to compile a C++ program on Mac OSX 10.9 via the command line (and clang) that uses opencv (version 2.4.12) and am running into some problems.
I'm trying to compile the program by running the following in Terminal:
clang --std=c++11 -stdlib=libc++ test.cpp -L/usr/local/Cellar/opencv/2.4.12/lib -lopencv_core.2.4.12 -lopencv_ml.2.4.12 -lopencv_video.2.4.12 -lopencv_ml -lopencv_video -lc++
However I'm getting the following error:
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
Along with a string of other opencv functions that can't be found.
Upon doing some additional research, I found out that the problem might be associated with using libc++ instead of libstdc++ as the library. I tried switching to libstdc++ and was stuck with the following error.
fatal error: 'array' file not found
#include <array>
I researched that error and found out that for the Mac, it was because of using libstdc++ instead of libc++. Essentially, I'm kind of stuck in a loop and need some help compiling this program. Any thoughts?

Related

Boost undefined symbol for architecture arm64

I'm trying to build a project that contains code with boost 1.65.1 required.
I'm on a Mac OS M1 chipset (arm64)
I downloaded boost using homebrew
brew install boost
and the version 1.80.0 was correctly installed.
I complice using gcc g++ (and not clang)
I include the library in my cmake and all the includes are working fine and my IDE is able to find all the function references.
At some part of the code I'm using
boost::this_fiber::sleep_for function,
and at the compilation an error is raised by the linker
Undefined symbols for architecture arm64:
"_boost::fibers::context::wait_until(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long long, std::ratio<1l, 1000000000l> > > const&)", referenced from:
_void boost::this_fiber::sleep_for<long long, std::ratio<1l, 1000000l> >(std::chrono::duration<long long, std::ratio<1l, 1000000l> > const&) in libTest.a
ld: symbol(s) not found for architecture arm64
in my case I find the boost package and linked all boost libraries.
I even tried by hand to add -lboost_fiber-mt or Boost::fiber and I always end up with the same error.
Do you have any idea why the symbol is undefined ? The same code works perfectly on a ubuntu x86_64.
I tried include the libraries by hand in the make and symbols is always undefined.
Probably not quite the answer you're looking for unfortunately, but I've had the same experience with a brew install, but have had success building the boost libraries myself, downloading from here and following installation instructions here. Hope this is helpful!

Trying to use Boost C++ Library on MacOS

I've been trying for hours to get Boost C++ libraries working on MacOS 10.13.6.
I'm up-to-date with Xcode and Homebrew.
I used brew install boost to install boost and followed the Boost Getting Started instructions for help with compiling.
I've tried everything that I can think of and read every post I can find on StackOverflow about this.
I'm attempting to compile with:
g++ -std=c++17 -stdlib=libc++ -I /usr/local/opt/boost/include -L /usr/local/opt/boost/lib ass3.cpp -o ass3 -lboost_filesystem
and I get the following error message:
Undefined symbols for architecture x86_64: "boost::system::detail::generic_category_ncx()", referenced from: boost::system::generic_category()
in ass3-3dc386.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I haven't yet tried to install Boost from source.
If I exclude -std=c++17 the code will compile with a warning that I am using c++11 alias. As I get further into my project I'll be using c++17 specific features so I need to include this as far I as I know.
Compiler warning:
warning: alias declarations are a C++11 extension [-Wc++11-extensions]
What am I missing?

shared_timed_mutex not available on OS X 10.11.2?

I'm trying to play around with the new shared_timed_mutex structures from C++ 14 on OS X 10.11.2 using Eclipse CDT 4.5.0. Xcode is 7.2. I'm using GCC C++ with the following options:
-O0 -g3 -Wall -c -fmessage-length=0 -std=c++14
Her's what I get:
Invoking: MacOS X C++ Linker
g++ -o "MyProject" ./src/main.o
Undefined symbols for architecture x86_64:
"std::__1::shared_timed_mutex::shared_timed_mutex()", referenced from:
HashMap<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<int> >::HashMap() 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)
Here's my clang version info:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
I've searched my way through the web and found a couple of recommendations, for example this one on Reddit:
https://www.reddit.com/r/cpp_questions/comments/3ejfkr/is_c14s_stdshared_timed_mutex_available_on_os_x/?
This is very close to my problem, I've tried the suggestions, but I´m hoping, that I won´t have to do the manual libc++ download, but could stick with the standard libs shipped with my system.
Thank you!
For the past two years (as I write this) Apple has updated the libc++ headers for their tools releases, but not the libc++ sources. And libc++ implements std::shared_time_mutex both in <shared_mutex> and in shared_mutex.cpp.
According to Apple dev presentation the shared_timed_mutex will be available starting with macOS 10.12. See page 52

How to build simple OpenCV program

I am trying to build the program at http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html after installing OpenCV on my machine. I try to compile it using g++ display_image.cpp -o DisplayImage but I get the following error:
display_image.cpp:(.text+0x9d): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
I think this has to do with how I'm compiling the program (some unresolved reference to opencv2) but I don't know how to resolve it. I'm using Ubuntu 13.04. Thanks!
Edit: I realize this is similar to OpenCV 2.3 Compiling Issue - Undefined Refence - Ubuntu 11.10 but I don't really understand the use of pkg-config opencv --cflags --libs and how to avoid it
You got a linker error because you didn't specify OpenCV on your GCC command line
Follow that tutorial (http://opencv.willowgarage.com/wiki/CompileOpenCVUsingLinux) link your sample with OpenCV

dylib on Snow Leopard "file is not of required architecture"

I have compiled opencv on snow leopard and it says it compiled correctly, however when I try to compile my sample program against it, I get output like:
g++ -o tm_scons template.o -L/opencv/opencv/build/lib -lcxcore -lcv -lcvaux -lhighgui -lml
ld: warning: in /opencv/opencv/build/lib/libcxcore.dylib, file is not of required architecture
ld: warning: in /opencv/opencv/build/lib/libcv.dylib, file is not of required architecture
ld: warning: in /opencv/opencv/build/lib/libcvaux.dylib, file is not of required architecture
ld: warning: in /opencv/opencv/build/lib/libhighgui.dylib, file is not of required architecture
ld: warning: in /opencv/opencv/build/lib/libml.dylib, file is not of required architecture
Is this likely a problem with my compilation of OpenCV or of my app that is using it?
Compiling correctly does not mean compiling for whatever architecture you're trying to compile now. Most likely the library is compiled as 32-bit and you're compiling as 64-bit or vice-versa.
Most likely your program is compiled 64-bit (the default behavior on SnowLeopard) and the library is compiled 32-bit. Try adding the -m32 or -arch i386 flag to the compiler when you build your program and see if the link works.
As it turns out the magic was using -m32 and switching to the /usr/bin/g++-4.0 compiler.
Ugh....