C++ strange behaviour - c++

I am very new to C++ to say the least. However, I cannot find answer to this specific question anywhere.
This is not code specific either, as I tried downloading a lot of code and compiling it via g++ or just make.
It seems that whenever i include a class header in my main() it throws the linker error:
Undefined symbols for architecture x86_64:
"House::getNumBath()", referenced from:
printHouse(House) in houses-91268b.o
"House::getSqft()", referenced from:
printHouse(House) in houses-91268b.o
"House::getColor()", referenced from:
printHouse(House) in houses-91268b.o
"House::House(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, int, double)", referenced from:
_main in houses-91268b.o
"House::House()", referenced from:
_main in houses-91268b.o
"House::~House()", referenced from:
_main in houses-91268b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It happens no matter what code do I download or produce and seems to be related to the constructor function implemented as MyClass::MyClass(/* args */);
Someone suggested that I should include .cpp file in my main instead of .h file, and yes it works. But it's very wrong as I've always been taught that interfaces are meant for it. Otherwise what's their purpose?
With another person suggestion (googled), I am now downloading 10GB worth of Xcode, and hope it helps. Still seems like it's not going to work. What is wrong with C++ here? Am I for some reason missing C++ 11 (again, why on the relatively new OS?) ?
UPDATE:
I grabbed the code from another tutorial that throws me the same thing:
dog.cc
#include "Dog.h"
Dog::Dog() {
}
dog.h
#ifndef DOG_H
#define DOG_H
class Dog {
public:
Dog();
protected:
private:
};
#endif
main.cc
#include "Dog.h"
int main() {
Dog barky;
return 0;
}
Throws both the same after me doing (they are in the same folder, those):
g++ main.cc
or
make ./main
It is probably the simplest example I wanted to try, as I never had problems like this before in C, either using Makefile or by just cc the file.

You need to compile the object (dog.o) file first:
g++ -c dog.cc
Then compile the main with it:
g++ main.cc dog.o

Related

How can I fix g++ Undefined symbols for architecture x86_64 error?

First of all, I've read the related content on Stackoverflow on this problem but I still can't solve it. I've simplified my code as much as possible.
I've only a custom class with .h and .cpp files but I get error while trying to create an instance of this class from main.cpp.
main.cpp
#include "Customer.h"
using namespace std;
int main() {
Customer a("string");
return 0;
}
Customer.h
using namespace std;
class Customer {
public:
Customer(string input);
};
Customer.cpp
#include "Customer.h"
using namespace std;
Customer::Customer(string input) {
}
The error message I get is the following?
gcc *.cpp -o k
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
_main in main-40340f.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in main-40340f.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
_main in main-40340f.o
"std::terminate()", referenced from:
___clang_call_terminate in main-40340f.o
"___cxa_begin_catch", referenced from:
___clang_call_terminate in main-40340f.o
"___gxx_personality_v0", referenced from:
_main in main-40340f.o
Dwarf Exception Unwind Info (__eh_frame) in main-40340f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I run Mac OS X 10.9 and Sublime Text 3. gcc -v gives the following:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
It compiles without problem when I write empty constructors instead of these.
What could cause this problem?
Two problems:
You should #include <string>.
To compile C++ code on OS X, use clang++ or g++.
Use g++ instead of gcc:
$ g++ *.cpp -o k
and then the compiler driver knows to link-in the C++ runtime library. Given it's actually clang you are using, and not gcc, then this is better:
$ clang++ *.cpp -o k
Use "g++ "instead of "gcc" to compile and link your application. It automatically knows about the C++ libraries that need to be included.
You need to install Command Line Tools.
https://developer.apple.com/downloads/
g++ main.cpp(or Customer.cpp) -o k is not enough. You should compile them at the same time.
"g++ main.cpp Customer.cpp (and other *.cpp(s) if you have) -o target "
will work well.

Getting linker error when trying to import lua headers into C++ project?

I'm trying to write some C++ functions that can be run from Lua. However, when I try to import the header files, I get the following error:
Undefined symbols for architecture x86_64:
"_luaL_loadfilex", referenced from:
_main in main.o
"_luaL_newstate", referenced from:
_main in main.o
"_luaL_openlibs", referenced from:
_main in main.o
"_lua_close", referenced from:
_main in main.o
"_lua_pcallk", referenced from:
_main in main.o
"_lua_pushcclosure", referenced from:
_main in main.o
"_lua_setglobal", 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)
I've already added the file path to the Header Search Paths option in the Build Settings.
Here is the import code:
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
What am I doing wrong?
For a first working version, get the library here, extract the header files and the .a file into the same directory as the project file which has the code you posted in your question, then try to compile and link normally.
You may keep the references locally as in your question:
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
Later you refine your environment if that is needed.
The error you are getting is a link error not a compile error. The linker (called "ld") is complaining that it can't resolve symbols related to Lua. Make sure you have -llua52 in your link command so your library links to the Lua shared library (might be -llua or -llua5.2 on your system), and tell the linker where to find that lib via -Lpath/to/Lua/lib/folder.

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

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!
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.
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.
I am getting the same error. I don't think it is a "fix", but my work-around is to also include the cpp file. So instead of just putting
#include "MyClass.h"
I have to put
#include "MyClass.h"
#include "MyClass.cpp"
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.
When I using clang to compile a C++ program, I have the similar error.
But after I changing to use chang++, the compilation worked.

Problems with BigInt Library in C++

So I'm trying out this bigint library: https://mattmccutchen.net/bigint/ on OSX Lion.
I tried making a simple file using the example on their site.
#include <iostream>
#include <bigint/BigIntegerLibrary.hh>
using namespace std;
int main(){
BigInteger a;
int b=5;
a=b;
cout<<a;
return 0;
}
When I compile this in textmate, the output is:
Undefined symbols for architecture x86_64:
  "BigInteger::BigInteger(int)", referenced from:
      _main in ccl9yNN5.o
  "BigInteger::operator=(BigInteger const&)", referenced from:
      _main in ccl9yNN5.o
  "operator<<(std::basic_ostream<char, std::char_traits<char> >&, BigInteger const&)", referenced from:
      _main in ccl9yNN5.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Can anyone tell me what is wrong?
just in case anyone is still looking for answers to this. I found that you have to dig into the Makefile a little bit and figure out how to manually link the program using the Makefile. What I did is in the bigint folder, I ran "make library" once, then later on, I followed the samples at the very end of the Makefile to link the library files together. Here is my Makefile trying to solve an SPOJ problem:

Link with Cocoa Framework from C++ and Objective-C++

I'm trying to use a Cocoa Framework (MultitouchSupport, to be specific) from within a basic Objective-C++ file, but I keep getting undefined symbol errors, as if g++ is supposed to have different linker flags than gcc.
My ultimate goal is to integrate a C++ networking library with some basic Objective-C code I got from here: http://steike.com/code/multitouch/.
When I run this to compile the original code, it works fine:
gcc -F/System/Library/PrivateFrameworks -framework MultitouchSupport test.m -o test -std=c99
But when I rename the file to test.mm, so that it can later include and reference C++ files, the following doesn't work:
g++ -F/System/Library/PrivateFrameworks -framework MultitouchSupport test.mm -o test
And gives me these errors:
Undefined symbols for architecture x86_64:
"MTDeviceCreateDefault()", referenced from:
_main in ccq0vzuM.o
"MTRegisterContactFrameCallback(void*, int (*)(int, Finger*, int, double, int))", referenced from:
_main in ccq0vzuM.o
"MTDeviceStart(void*, int)", referenced from:
_main in ccq0vzuM.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [testpp] Error 1
What do I need to do in order for this Objective-C++ file to see the Framework I'm looking for so I can use C++ with it?
Thanks!
In the header that declares those three functions, is there an extern "C" block that wraps them? Something like this?
#ifdef __cplusplus
extern "C" {
#endif
// function declarations here
#ifdef __cplusplus
}
#endif
If not: You could add one to the header file, or add a similar wrapper around your #import of that header file.