对std :: ofstream :: open(const std :: string&,std :: _ Ios_Openmode)的未定义引用

时间:2015-05-21 17:09:40

标签: c++ linux

下面的代码在Windows中完美运行,但在Red Hat Enterprise Linux Server 5.9版(Tikanga)上却没有。

我首先必须安装gcc44-c++.x86_64,这已经解决了一些问题。

但是当我尝试通过g++44 -std=c++0x MyProgram.c -lpthread -o MyProgram编译我的C ++代码时(我也在使用线程),我遇到了以下消息:

/tmp/ccbL2a6z.o: In function `writeLog(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
MyProgram.c:(.text+0x1eb): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
collect2: ld returned 1 exit status

这是可能导致问题的代码(仅提取整个代码):

#define _GLIBCXX_USE_NANOSLEEP 
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <stdio.h>
#include <vector>
#include <fstream> 

//function to create timestamp
const std::string getTimestamp() {
    time_t now = time(0);
    struct tm tstruct = *localtime(&now);
    char timestamp[80];
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d.%X", &tstruct);
    return timestamp;
}

//function to write into logfile
void writeLog(std::string output){
    std::ofstream logfile;
    logfile.open(logpath, std::ios::out | std::ios::app);
    logfile << output << "\n";
    logfile.close();
}

0 个答案:

没有答案