首页 > 解决方案 > 创建指向类的指针时对类和函数的未定义引用。项目是用 CMake 和 g++ 构建的

问题描述

我有一个项目,我使用 cmake 和命令行 g++ main.cpp -o main 构建它们都返回错误:未定义的引用但指向不同的对象。

汽车.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

};

汽车.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;};             

模型X.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;
};

模型X.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; 
};          

所以基本上只是一个父类和一个子类。我把所有的源代码放在一个文件夹里,还有 CMakeLists.txt 在那里。

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)

如果我输入 g++ main.cpp -o main,我有这个错误:

/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

如果我输入 cmake --build 。在构建文件夹中,然后我有以下错误:

/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/是什么导致了这些错误?2/为什么 cmake 和 g++ 返回不同的错误?

标签: c++cmake

解决方案


推荐阅读