My code compiles and runs fine when I incorporate all of this as a single file. However, when I use a header file and use separate files, I get this error:
Undefined symbols for architecture x86_64:
"someClass::newNode()", referenced from:
_main in check.o
someClass::insert(someClass::Node*, char const*, char const*) in entry.o
ld: sym
bol(s) not found for architecture x86_64
I have tried everything and I cannot find what the issue is. They compile separately using "-c" but linking the object files gives me the error. Also, I am using inclusion guards and all the suggested tips when including header files. Any help would be appreciated!
//.h file
class someClass{
public:
//other stuff
struct Node
{
//...
};
Node *newNode();
};
//entry.C
Node someClass::newNode(){
someClass::Node *bNode = new someClass::Node;
//...
return bNode;
}
//check.C
int main(){
//...
someClass obj;
someClass.Node *root = obj.newNode();
return 0;
}
For getting nested type in c++ use "::" instead of "."
someClass::Node *root = obj.newNode();
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
This is my error when trying to compile:
Undefined symbols for architecture x86_64:
"Model::Model(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<int, std::__1::allocator<int> >)", referenced from: ____C_A_T_C_H____T_E_S_T____4() in tests.cpp.o
I have read that std::string is a typedef of std::basic_string and I'm guess that std::vector is a typedef of... std::vector and std::allocator? or something...
Anyway. I have a Settings class that holds my settings and a Model class that contains my model. The code I am attempting to run is as below, and I am unable to compile it. the issue is with it not recognising my model constructor.
Settings s = Settings("../config/settings.json");
std::string mf = s.get_model_file();
std::vector<int> vec = s.get_input_shape();
Model m = Model(mf, vec);
For reference here is my Model class header:
class Model {
public:
Model(std::string model_file, std::vector<int> input_shape);
~Model();
double* get_ref_to_input_buffer();
std::string predict();
private:
std::string _model_file;
fdeep::model _model;
fdeep::shape3 _input_shape;
int _input_size;
double* _input_buffer;
fdeep::tensor3s _result;
void _load_model();
void _set_input_size(std::vector<int> input_shape);
void _set_input_shape(std::vector<int> input_shape);
void _create_input_buffer();
std::string _result_to_string();
};
and my Model class constructor:
Model::Model(std::string model_file, std::vector<int> input_shape) {
_model_file = model_file;
_load_model();
_set_input_size(input_shape);
_set_input_shape(input_shape);
}
These are the function being called in the constructor:
void Model::_load_model() { _model = fdeep::load_model(_model_file); }
void Model::_set_input_size(std::vector<int> input_shape) {
int total = 1;
for (std::vector<int>::iterator it = input_shape.begin();
it != input_shape.end(); ++it) {
total *= *it;
}
_input_size = total;
}
void Model::_set_input_shape(std::vector<int> input_shape) {
_input_shape = fdeep::shape3(input_shape[0], input_shape[1], input_shape[2]);
}
If anyone could point out where I'm going wrong or send me in the direction of what I need to read / learn that would be great. Thank you!
According to http://www.cplusplus.com/forum/general/55587/
"Undefined Symbols" generally indicates a linking issue. Are you definitely linking correctly to the appropriate library?
Your code compiles fine. The linker then goes looking in the libraries for the right functions, and since you compiled for architecture x86_64, it looks for the right libraries, similarly compiled - and finds none. This suggests (unless you've simply forgotten to link to the 64 bit library) that you've got a 32 bit version of the library. Your choices are:
Build your code for 32 bit, so you can use the libraries you've got.
Get the library source code, and compile it yourself for x86_64
Go looking wherever you found the library, and hope there's an x86_64 version sitting next to the 32 bit version you found last time.
By taking #Aconcagua 's advice I output the linker command from cmake using:
make tests VERBOSE=1
which gave me:
usr/bin/g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/tests.dir/tests.cpp.o CMakeFiles/tests.dir/__/Settings.cpp.o -o ../../../bin/tests
Noticing that this did not have a Model.cpp.o file I checked CMakeLists.txt and found:
add_executable(tests tests.cpp ../Settings.cpp ../Model.hpp)
Note that for Model I have entered the header file not the source file. Changing it to this:
add_executable(tests tests.cpp ../Settings.cpp ../Model.cpp)
Fixed my problem.
I have seen many related questions to this problem, but after carefully following advice from members, my problem still persists. The code is quite simple. I only have the following header file ("instrument.h"), which contains the base class and the template class:
#include <stdio.h>
#include <string>
using namespace std;
class Instrument
{
public:
Instrument();
virtual void print() const = 0;
};
template <class parameter> class Equity : public Instrument
{
public:
Equity();
virtual void print() const;
};
Now, in my main function on main.cpp I only do the following:
#include "instrument.h"
#include <iostream>
int main() {
Equity<double> pb;
return 0;
}
Well, I get the very well-known error:
Undefined symbols for architecture x86_64:
"Equity<double>::Equity()", 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 have already changed in Build Settings the C++ standard library to libstdc++, also to default compiler, and so on. Do I have a problem with my project settings? Is perhaps the template wrongly implemented? I was thinking I should also have a instrument.cpp file, but then again definitions for templates must be kept in the header file so that would probably crash too.
Thanks in advance
You declared the default constructors for both Instrument and Equity but defined them nowhere.
Alter their definitions appropriately:
public:
Equity() = default; // Or {} in pre-C++11
// ^^^^^^^^^
(And equivalently for Instrument)
You can also completely omit the declarations of any default constructors for now since you didn't declare any other constructors in both Equity and Instrument and the default constructors will be generated automatically.
I created a static library that includes the follow C++ files:
//TestClass.h File:
#ifndef TESTCLASS_H_
#define TESTCLASS_H_
using namespace std;
#include <string>
class TestClass
{
public:
TestClass();
virtual ~TestClass();
int sum(int x, int y) const;
string chain(const string& x, const string& y) const;
};
#endif /* TESTCLASS_H_ */
//TestClass.cpp File:
#include<iostream>
#include "TestClass.h"
TestClass::TestClass()
{
}
TestClass::~TestClass()
{
}
int TestClass::sum(int x, int y) const
{
return x+y;
}
//Test.cpp File:
string TestClass::chain(const string& x, const string& y) const
{
return x+y;
}
int main(int argc, char* argv[])
{
TestClass test;
cout << "1+1 = " << test.sum(1,1) << endl;
cout << "Dog+Cat = " << test.chain("Dog","Cat") << endl;
return 0;
}
I added
-x objective-c++
flag in "Compile Source" and
-lstdc++
flag in "Info.plist Other Preprocessor flags".
When I link my just created static library (with Objective C wrapper files), I receive the 4 follow errors, that I don't have any idea how to fix it:
Undefined symbols for architecture arm64:
"vtable for __cxxabiv1::__class_type_info", referenced from:
typeinfo for TestClass in libPredictionComplete.a(TestClass.o)
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"operator delete(void*)", referenced from:
TestClass::~TestClass() in libPredictionComplete.a(TestClass.o)
"___gxx_personality_v0", referenced from:
-[CppObject init] in libPredictionComplete.a(CppObject.o)
-[PredictionComplete init] in libPredictionComplete.a(PredictionComplete.o)
-[PredictionComplete chain::] in libPredictionComplete.a(PredictionComplete.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'll appreciate any ideas about.
From my experience, Xcode will fail to link against C++ libraries when there are no C++ sources present in the top-level project. This is also true in pure Obj-C projects as well where you are linking against static libraries that have C++ code.
A less invasive solution is to simply include an empty C++ source file in your project (here's an example cpp stub) that is "compiled" into the final .app binary. It actually doesn't emit any code, but it makes Xcode aware that there is C++ code present, causing C++ libraries to be linked in.
The advantage of this solution is it avoids modifying the project settings in Xcode, so no need to add special linker flags to force Xcode to do the right thing.
I fixed it when I added the "-lstdc++" compile flag to "other linker flags" section in the Swift Project itself and not only in the static library project with c++ files.
The -lstdc++ should be in "Other Linker Flags" (or OTHER_LDFLAGS). Note that that will only work if the "C++ Standard Library" is set to libstdc++.
Also of note is that Xcode is usually smart enough to include the standard C++ library if the target has any C++ source code in it, so you don't need to explicitly link to either libstdc++ or libc++.
You should have -x c++ rather than objective-c++. There is, however, no need to specify this flag if you name your C++ sorce *.cpp.
I am using templates for the first time in C++ and running into a problem when I try to compile. Basically trying to create my own basic ArrayList of sorts:
The .hpp:
#ifndef ARRAYLIST_HPP_
#define ARRAYLIST_HPP_
template <class T>
class ArrayList
{
private:
int current, top ;
T * al ;
public:
ArrayList() ; // default constructor is the only one
};
#endif /* ARRAYLIST_HPP_ */
The .cpp:
#include "ArrayList.hpp"
using namespace std ;
//template <class T>
//void memoryAllocator( T * p, int * n ) ; // private helper functions headers
template <class T>
ArrayList<T>::ArrayList()
{
current = 0 ;
top = 10 ;
al = new T[top] ;
}
The main:
#include "ArrayList.hpp"
int main()
{
ArrayList<int> test ;
}
When I try to build without the main it compiles fine, however, as soon as I try to use it in the main I get the following error:
Undefined symbols for architecture x86_64:
"ArrayList<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::ArrayList()", referenced from:
_main in Website.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [APProject2] Error 1
Any thoughts as to what might be the problem would be much appreciated!
Cheers!
Templates need to be declared and defined in the header.
Also: I don't think that is the real error. It mentions an instantiation of ArrayList<std::string> which I can't see anywhere.
This FAQ entry explains why.
You need to include the implementation in the .hpp file. The compiler needs to know T at compile time to generate the specialization.
You can't put the templated code into a separate .cpp file... your constructor should be in the ArrayList header. The point is that it's only when main() is compiled that the compiler realises it needs to instantiate ArrayList and what type T will take, and so it needs to have the code available to do the instantiation....
I've recently moved over to a mac, and am struggling using the command line compilers. I'm using g++ to compile, and this builds a single source file fine. if I try to add a custom header file, when I try to compile using g++ I get undefined symbols for architecture i386. The programs compile fine in xCode however. Am I missing something obvious?
tried using g++ -m32 main.cpp... didn't know what else to try.
Okay, The old code compiled... Have narrowed it down to my constructors.
class Matrix{
public:
int a;
int deter;
Matrix();
int det();
};
#include "matrix.h"
Matrix::Matrix(){
a = 0;
deter = 0;
}
int Matrix::det(){
return 0;
}
my error is
Undefined symbols for architecture x86_64:
"Matrix::Matrix()", referenced from:
_main in ccBWK2wB.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
my main code has
#include "matrix.h"
int main(){
Matrix m;
return 0;
}
along with the usual
It looks like you’ve got three files:
matrix.h, a header file that declares the Matrix class;
matrix.cpp, a source file that implements Matrix methods;
main.cpp, a source file that defines main() and uses the Matrix class.
In order to produce an executable with all symbols, you need to compile both .cpp files and link them together.
An easy way to do this is to specify them both in your g++ or clang++ invocation. For instance:
clang++ matrix.cpp main.cpp -o programName
or, if you prefer to use g++ — which Apple haven’t updated in a while, and it looks like they won’t in the foreseeable future:
g++ matrix.cpp main.cpp -o programName
is not the case here, but it may happen to be the you forget to put the class name with ::
for example:
a good format:
foo.h
class Foo{
public:
Foo();
void say();
private:
int x;
};
foo.cpp
Foo::Foo(){
this->x = 1;
}
void Foo::say(){
printf("I said!\n");
}
a bad format
foo.h
class Foo{
public:
Foo();
void say();
private:
int x;
}
foo.cpp
Foo::Foo(){
this->x = 1;
}
//I always mistake here because I forget to put the class name with :: and the xcode don't show this error.
void say(){
printf("I said!\n");
}
Did you actually define the Box constructor somewhere? (like Line.cpp)