g++ ld: symbol(s) not found for architecture x86_64

C++OpencvGcc

C++ Problem Overview


I'm trying to compile the Sam Hare's Struck code.

I'm using mac OSX10.9, opencv 2.4.6 and Eigen 2.0.17.

Eigen and opencv headers are stored in /opt/local/include while opencv dylib in /opt/local/lib.

I modified the Hare's Makefile to work on this folder. When I type make on the terminal:

g++ -L/opt/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc src/Config.o src/Features.o src/HaarFeature.o src/HaarFeatures.o src/HistogramFeatures.o src/ImageRep.o src/LaRank.o src/MultiFeatures.o src/RawFeatures.o src/Sampler.o src/Tracker.o src/main.o src/GraphUtils/GraphUtils.o -o struck

I get these errors:

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:
      _main in main.o   "cv::split(cv::Mat const&, std::__1::vector<cv::Mat, std::__1::allocator<cv::Mat> >&)",
referenced from:
      ImageRep::ImageRep(cv::Mat const&, bool, bool, bool) in ImageRep.o   "cv::imread(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
int)", referenced from:
      _main in main.o   "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&,
cv::_InputArray const&)", referenced from:
      LaRank::Debug() in LaRank.o
      Tracker::Debug() in Tracker.o
      _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 ideas? Thanks!

C++ Solutions


Solution 1 - C++

I had a similar warning/error/failure when I was simply trying to make an executable from two different object files (main.o and add.o). I was using the command:

gcc -o exec main.o add.o

But my program is a C++ program. Using the g++ compiler solved my issue:

g++ -o exec main.o add.o

I was always under the impression that gcc could figure these things out on its own. Apparently not. I hope this helps someone else searching for this error.

Solution 2 - C++

finally solved my problem.

I created a new project in XCode with the sources and changed the C++ Standard Library from the default libc++ to libstdc++ as in this and this.

Solution 3 - C++

I wrote my declarition in the .h file

class String
{
    String (const char * cstr = 0);
}; 

and I used inline in another .cpp file (implementation)

inline String::String(const char * cstr)
{
//code ... 
}

then I use g++ and get this: Undefined symbols for architecture x86_64: "String::String(char const*)", referenced from: _main in test-d389f3.o ld: symbol(s) not found for architecture x86_64

solution: do not write inline when declarition and implementation is independent.

Solution 4 - C++

When I using clang to compile a C++ program, I have the similar error.

But after I changing to use chang++, the compilation worked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmbProView Question on Stackoverflow
Solution 1 - C++NateView Answer on Stackoverflow
Solution 2 - C++mbProView Answer on Stackoverflow
Solution 3 - C++PatZView Answer on Stackoverflow
Solution 4 - C++LittleNewtonView Answer on Stackoverflow