makefile:找不到架构的符号

时间:2012-04-01 22:26:53

标签: c++ makefile g++

我正在尝试用makefile解决问题。

我有一个main.cpp,然后是addOn.cpp和addOn.hpp。 我只是想用简单的makefile来编译它。

我有:

    main: Main.o Log.o
        g++ -o main Main.o Log.o

    Main.o: main.cpp  Log.hpp
        g++ -c main.cpp

    Log.o: Log.cpp Log.hpp
        g++ -c Log.cpp

但继续得到:

    $ make
    g++ -c main.cpp
    g++ -c Log.cpp
    g++ -o main Main.o Log.o
    Undefined symbols for architecture x86_64:
      "Log::~Log()", referenced from:
          _main in Main.o
      "Log::overallVerboseLvl", referenced from:
          Log::setLevel(int)  in Log.o
          Log::Log()   in Log.o
          Log::Log()   in Log.o
          Log::operator<<(char const*)in Log.o
      "Log::fileName", referenced from:
          Log::setFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in Log.o
          Log::Log(int)in Log.o
          Log::Log(int)in Log.o
          Log::Log()   in Log.o
          Log::Log()   in Log.o
    ld: symbol(s) not found for architecture x86_64
    collect2: ld returned 1 exit status
    make: *** [main] Error 1

以下是文件
main.cpp

#include <iostream>
#include <string.h>
#include "Log.hpp"

int main(int argc, char *argv[]) {
    Log::setFile("output.log"); 
    Log(Log::LEVEL_INF) << "out and up..."; 
    Log::setLevel(Log::LEVEL_INF); 
    Log(Log::LEVEL_INF) << "another line"; 
    Log(Log::LEVEL_ERR) << "now a number: " << 13; 
    Log(Log::LEVEL_DBG) << "I shall not pass";
    return 0;
}

Log.hpp

#ifndef log_Log_hpp
#define log_Log_hpp
#include <string.h>

class Log{
    private:
        static std::string fileName;
        static int overallVerboseLvl;
        int messageVerboseLvl;
    public:
    static const int LEVEL_INF;
    static const int LEVEL_ERR;
    static const int LEVEL_DBG;

    Log(int verboseLvl);
    Log();
    ~Log();
    std::ostream& operator<<(char const*);
    static void setFile(std::string file);
    static void setLevel(int level);

};


#endif

Log.cpp

#include <iostream>
#include <string.h>
#include "Log.hpp"

const int Log::LEVEL_INF = 0;
const int Log::LEVEL_ERR = 1;
const int Log::LEVEL_DBG = 2;

void Log::setLevel(int level){
    Log::overallVerboseLvl = level;
}

void Log::setFile(std::string fileName ){
    Log::fileName = fileName;
}

Log::Log(){
    Log::fileName = "";
    Log::overallVerboseLvl = Log::LEVEL_DBG;
}

Log::Log(int verboseLvl){
    Log::fileName = "";
    Log::messageVerboseLvl = verboseLvl;
}

std::ostream& Log::operator<<(char const* text){
    if (Log::overallVerboseLvl >= Log::messageVerboseLvl) {
        std::cout << text;
    }     
    return std::cout;
}

我做错了什么?

3 个答案:

答案 0 :(得分:5)

您尚未定义析构函数或缺少的静态变量。添加其中一个:

Log::~Log() {
}

静态变量部分有点不直观。你已经声明了它们,但还没有定义它们。您可以这样想 - 所有编译器都知道您希望能够使用这些变量。但是说你包括Log.h 100次。您不希望编译器创建100个相同变量的副本。所以你必须给它一个实际实例化的地方。在Log.cpp文件中,执行以下操作:

// Note, no 'static' keyword needed (or allowed).
std::string Log::fileName;
int Log::overallVerboseLvl;

答案 1 :(得分:3)

有一个干净的目标,运行make clean命令,然后尝试make。我想你可能有一些来自不同操作系统上的编译的.o文件

clean:
     <TAB> rm *.o

答案 2 :(得分:1)

您尚未定义链接器抱怨的内容。你已经声明了~Log()但是Log.cpp中没有这样的方法。同样,必须在Log.cpp中定义静态变量filenameoverallVerboseLvl