我試圖解決的問題與makefile文件。生成文件:符號(S)未找到架構
我有一個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;
}
我在做什麼錯?
您能告訴我們您的源文件嗎? – templatetypedef 2012-04-01 22:29:17
我注意到在錯誤輸出中提到的文件'Log.o'。這也在你的makefile中嗎? – thb 2012-04-01 22:31:11
是否有一個名爲Log.cpp的文件?你在同一個目錄下有兩個makefile嗎? – gbulmer 2012-04-01 22:31:32