I have a project, that I build with cmake and with command line g++ main.cpp -o main They both returns error: undefined reference but to different objects.
car.h
#include "upgrade.h"
#include "Usage.h"
class Car { // abstract class
public:
Car();
virtual ~Car();
void getProperty(); // test void function
virtual int run ( int &mode ); // test function return value, and receives pass by reference parameter
virtual int fuelType ( int &type ); // test function return value, and receives pass by pointer
virtual int upgrade ( Upgrade *upgrade); // test function accepting a class pointer as parameter;
virtual int specialUse ( Usage use); // test function accepting a class object as parameter;
virtual int multiTask ( int speed); // test function running on multi-thread
virtual int maintain ( int run); // test function returning error code in errono
static int carBasicProperty;
private:
char * model [100]; // test per character manipulation
int mode; // car runs and sets run mode as normal or sport
};
car.cpp
#include "car.h"
Car::Car(){};
virtual Car:: ~Car(){};
virtual void Car::getProperty(){};
virtual int Car::run ( int &mode ){return 0;};
virtual int Car::fuelType ( int &type ){return 0;};
virtual int Car::upgrade ( Upgrade *upgrade){return ;};
virtual int Car::specialUse ( Usage use){return 0;};
virtual int Car::multiTask ( int speed){return 0;};
virtual int Car::maintain ( int run){return 0;};
modelX.h
#include "car.h"
class modelX : public Car{
public:
modelX(char* customer); // test parameterized construtor
virtual ~modelX(); // unittest requires virtual for destructor
void setProperty(int cost, int mode, int fuelType, int upgrade, int usage); // test void* function
void getProperty(); // test modifying function and test
virtual int run ( int &mode , int distance); // function overloaded the parent function
virtual int fuelType ( int *type ); // test function return value, and receives pass by pointer
virtual int upgrade ( Upgrade *upgrade); // test function accepting a class pointer as parameter;
virtual int specialUse ( Usage use); // test function accepting a class object as parameter;
virtual int multiTask ( int speed); // test function running on multi-thread
virtual int maintain ( int run); // test function returning error code in errono
private:
int m_cost;
int m_mode;
int m_fuelType;
char * m_customer;
};
modelX.cpp
# include "modelX.h"
# define NORMAL 100
# define SPORT 200
# define ECO 300
# define HYBRID 1
# define ELECTRICAL 2
# define GAS 3
modelX::modelX(char* customer): m_customer(customer){} ;
modelX:: ~modelX(){};
void modelX::getProperty(){};
void modelX::setProperty(int cost, int mode, int fuelType, int upgrade, int usage){
m_cost = cost;
m_mode = mode;
m_fuelType = fuelType;
};
int modelX::run ( int &mode , int distance){
switch (mode){
case NORMAL:
{
int fuelCost = 10;
for (int i = 1; i < distance; i++){
int fuelCost = +10;
};
return fuelCost;
};
case SPORT:
{
int fuelCost = 10;
for (int i = 1; i < distance; i++){
int fuelCost = +20;
};
return fuelCost;
};
case ECO:
{
int fuelCost = 10;
for (int i = 1; i < distance; i++){
int fuelCost = +5;
};
return fuelCost;
};
default:
return -1;
}
};
int modelX::fuelType ( int *type ){
return 0;
};
int modelX::upgrade ( Upgrade *upgrade){
return 0;
};
int modelX::specialUse ( Usage use){
return 0;
};
int modelX::multiTask ( int speed){
return 0;
};
int modelX::maintain ( int run){
return 0;
};
So basically just a parent class and a child class. I put all the source code in one folder, and also CMakeLists.txt there.
CMakeLists.txt
cmake_minimum_required (VERSION 3.10)
project (CAR VERSION 1.0)
#set build options or set C++ standard
set(CMAKE_CXX_STANDARD 11)
#set include paths (if sources has more subfolders)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
#add executable target name
add_executable(${PROJECT_NAME} main.cpp modelX.cpp upgrade.cpp usage.cpp)
If I type g++ main.cpp -o main, I have this error:
/usr/bin/ld: /tmp/ccrKjNks.o: in function `main':
main.cpp:(.text+0x32): undefined reference to `modelX::modelX(char*)'
collect2: error: ld returned 1 exit status
If I type cmake --build . in build folder, then I have this following errors:
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o: in function `modelX::modelX(char*)':
modelX.cpp:(.text+0x1c): undefined reference to `Car::Car()'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o: in function `modelX::~modelX()':
modelX.cpp:(.text+0x66): undefined reference to `Car::~Car()'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o:(.data.rel.ro._ZTV6modelX[_ZTV6modelX]+0x20): undefined reference to `Car::run(int&)'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o:(.data.rel.ro._ZTV6modelX[_ZTV6modelX]+0x28): undefined reference to `Car::fuelType(int&)'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o:(.data.rel.ro._ZTI6modelX[_ZTI6modelX]+0x10): undefined reference to `typeinfo for Car'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/CAR.dir/build.make:145: CAR] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/CAR.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
1/ what are causing those errors? 2/ why do cmake and g++ returns different errors ?