failed to link simple code - c++

i have this simple code in the following files:
Stat.h
#ifndef STAT_H
#define STAT_H
class Stat {
public:
void compute_value();
};
#endif
Stat.cpp
class Stat {
public:
void compute_value() {
}
};
main.cpp
#include "Stat.h"
int main(void)
{
Stat stat;
stat.compute_value();
}
When i try to compile i got the following error:
clang++ -std=c++14 -Wall -Wextra -pedantic -Weverything -O3 Stat.cpp main.cpp -o main
/tmp/main-0466d7.o: In function `main':
main.cpp:(.text+0xf6a): undefined reference to `Stat::compute_value()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You are redefining the class in your Stat.cpp source file because definition is also a declaration. Instead of having the entire class redefinition you only need to define the member function(s) in your Stat.cpp source file and include the Stat.h header:
#include "Stat.h"
void Stat::compute_value() {}

Related

Can't find the source of the error: 'Undefined symbols for architecture x86_64' Makefile

I am getting this error:
g++ -c location.cpp
g++ -c place.cpp
g++ -c popularplace.cpp
g++ -c person.cpp
g++ -o main main.cpp *.o
Undefined symbols for architecture x86_64:
"sim::PopularPlace::arraycounter", referenced from:
sim::PopularPlace::PopularPlace() in popularplace.o
sim::PopularPlace::PopularPlace(double, double) in popularplace.o
"sim::PopularPlace::places", referenced from:
sim::Person::action() in person.o
sim::PopularPlace::PopularPlace() in popularplace.o
sim::PopularPlace::PopularPlace(double, double) in popularplace.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
When building this makefile:
main: main.cpp simulation.h location.o place.o popularplace.o person.o
g++ -o main main.cpp *.o
location.o: location.cpp location.hpp simulation.h
g++ -c location.cpp
place.o: place.hpp place.cpp
g++ -c place.cpp
popularplace.o: place.hpp place.cpp simulation.h popularplace.hpp popularplace.cpp
g++ -c popularplace.cpp
person.o: person.cpp person.hpp location.hpp simulation.h place.hpp place.cpp location.cpp popularplace.o
g++ -c person.cpp
clean:
rm *.o
I can't find the error. Here is the popularplace.cpp file:
#include "popularplace.hpp"
#include "simulation.h"
namespace sim{
PopularPlace::PopularPlace()
:open(true){
places[PopularPlace::arraycounter] = this;
PopularPlace::arraycounter++;
}
PopularPlace::PopularPlace(double x, double y)
:Place(x, y), open(true){
places[PopularPlace::arraycounter] = this;
PopularPlace::arraycounter++;
}
bool PopularPlace::isOpen() const{
return open;
}
void PopularPlace::close(int numOfCasesToReopen){
open = false;
}
bool PopularPlace::reopen(int activeCases){
if(activeCases <= numOfCasesToReopen){
open = true;
return true;
} else{
return false;
}
}
}
and here's the popularplace.hpp file:
#ifndef POPULAR_PLACE_HPP
#define POPULAR_PLACE_HPP
#include "simulation.h"
#include "place.hpp"
namespace sim{
class PopularPlace : public Place{
private:
bool open;
int numOfCasesToReopen;
static int arraycounter;
public:
static PopularPlace* places[POPULAR_PLACES];
PopularPlace();
PopularPlace(double x, double y);
bool isOpen() const;
void close(int numOfCasesToReopen);
bool reopen(int activeCases);
};
}
#endif
If you need me to provide additional information/code, I'd be happy to do so. Thanks.
Static variables need a definition; they are declared in the header but they should also be defined in the cpp file. This is also where you'd initialize them. For example:
int PopularPlace::arraycounter{0};
PopularPlace* PopularPlace::places[POPULAR_PLACES] = {};
However, you might consider using an std::vector<std::shared_ptr<PopularPlace>> instead of a manually-managed array.

C++ issue with class declared in a separate object file

I've got a problem when declaring a class in a separate object file and then using it in the main function of another file:
main.cpp:
#include <helloclass.hpp>
using namespace std;
int main() {
Hello hi;
hi.hello();
return 0;
}
helloclass.cpp:
#include <iostream>
using namespace std;
class Hello {
public:
void hello() {
cout << "Hello world\n";
}
Hello() {}
};
helloclass.hpp:
class Hello {
public:
void hello();
Hello();
};
Then I ran the following commands:
g++ -I. -c main.cpp
g++ -c helloclass.cpp
g++ -o main main.o helloclass.o
However, the last command gives the following output:
main.o: In function `main':
main.cpp:(.text+0x1f): undefined reference to `Hello::Hello()'
main.cpp:(.text+0x2b): undefined reference to `Hello::hello()'
collect2: error: ld returned 1 exit status
To me, it seems like I'm missing something pretty obvious. Does anyone know how to fix this?
You should not redefine the class in the separate source file. Instead include the header-file and implement the Hello::hello function (and the Hello::Hello constructor).

Error Even after Explicit Instatantiation

I am trying to get a template code working with the header file and cpp file separated. I use explicit instantiation for this. But I still get an undefined reference error.
foo.h
template<typename T>
class Foo
{
public:
void f();
};
foo.cc
#include <iostream>
#include "Foo.h"
template<typename T>
void Foo<T>::f()
{
std::cout << "Foo<T>::f()\n";
}
template class Foo<int>;
main.cc
#include "foo.h"
int main()
{
Foo<int> x;
x.f();
}
When I compile:
g++ main.cc -o test
/tmp/ccfHjiVJ.o: In function `main':
main.cc:(.text+0x10): undefined reference to `Foo<int>::f()'
collect2: ld returned 1 exit status
The gcc version I use is gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
You've forgotten to compile foo.cc in your compile command, add foo.cc:
g++ main.cc foo.cc -o test
^^^^^^

Geany - undefined reference

I am trying to compile C++ code with Geany.
Compile command: g++ -Wall -c "%f"
Build command: g++ -Wall -o "%e" "%f"
main.cpp:
#include <iostream>
#include "Person.hpp"
int main()
{
Person p1(16);
std::cout << p1.getAge();
return 0;
}
Person.hpp
class Person
{
public:
Person(int a);
void setAge(int);
int getAge() const;
private:
int age;
};
inline int Person::getAge() const
{
return age;
}
Person.cpp
#include "Person.hpp"
Person::Person(int a)
{
age = a;
}
void Person::setAge(int a)
{
age = a;
}
Error:
g++ -Wall -o "main" "main.cpp" (in directory:
/home/me/projects/Test) /tmp/ccxYmWkE.o: In function main':
main.cpp:(.text+0x15): undefined reference toPerson::Person(int)'
collect2: error: ld returned 1 exit status Compilation failed.
Before Geany, I only used Code::Blocks and everything worked fine. How can I fix it?
It's obvious you didn't add Person.cpp to the compilation command. then it can not pass the linkage level.
Add -o Person Person.cpp to the build option after g++ -Wall -c "%e" "%f".
After all the compile command should be something like below:
g++ -Wall -o "main" "main.cpp" -o Person Person.cpp

C++ linking object's files (G++)

class.h
#include <iostream>
#include <stdint.h>
using namespace std;
template <typename T>
class CIntegerType {
public:
void Show ( void );
private:
T m_Data;
};
class.cpp
#include "class.h"
template <typename T>
void CIntegerType<T> :: Show ( void ) {
cout << m_Data << endl;
}
main.cpp
#include "class.h"
int main ( void ) {
CIntegerType<uint32_t> UINT32;
UINT32 . Show ();
return 0;
}
This commands return:
g++ -Wall -pedantic -c main.cpp
g++ -Wall -pedantic -c class.cpp
g++ -Wall -pedantic -o class.o main.o
main.o: In function `main':
main.cpp:(.text+0x11): undefined reference to 'CIntegerType< unsigned int>::Show()'
collect2: ld returned 1 exit status
Try putting your template implementation in the header file.
See: Why can templates only be implemented in the header file?
Try g++ -Wall -pedantic -o main.o class.o instead. You are facing the same problem as in this question: g++ linking order dependency when linking c code to c++ code
The linker searches for functions in the order they appear. Since you have a template function, its use in main must be fed to the linker prior to the actual code to instantiate it in class.