简体   繁体   English

g ++链接错误:“对'main'的未定义引用

[英]g++ link error: 'undefined reference to 'main'

I have 3 files; 我有3个档案; main.cpp (which contains main()), FileWriter.h, and FileWriter.cpp. main.cpp(包含main()),FileWriter.h和FileWriter.cpp。 I'm using g++ (version Debian 4.9.2-10) on Debian Jessie. 我在Debian Jessie上使用g ++(Debian 4.9.2-10版本)。 My project contains .cpp files in '/root/dev/Practice/src/', and a single header (FileWriter.h) in '/root/dev/Practice/include/'. 我的项目在/ root / dev / Practice / src /中包含.cpp文件,在/ root / dev / Practice / include /中包含单个标头(FileWriter.h)。 The compilation of the two object files works, but the linking to an executable complains about undefined reference to main(), although I do indeed have a seemingly valid one defined in 'main.cpp'. 虽然可以编译两个目标文件,但是链接到可执行文件会抱怨未定义对main()的引用,尽管我确实确实在'main.cpp'中定义了一个看似有效的文件。

Here's the output of my make file (which is in the root '/root/dev/Practice/' directory): 这是我的make文件的输出(位于根目录“ / root / dev / Practice /”中):

 g++ -c -g -Wall -o src/FileWriter.o src/FileWriter.cpp g++ -c -g -Wall -o src/main.o src/FileWriter.cpp g++ src/FileWriter.o src/main.o -o bin/Practice /usr/lib/gcc/i586-linux-gnu/4.9/../../../i386-linux-gnu/crt1.o: In function '_start'" /build/glibc-J1NNmk/glibc-2.19/csu/../sysdeps/i386/start.S:111: undefined reference to 'main' collect2: error: ls returned 1 exit status Makefile:10: recipe for target 'bin/Practice' failed 

Here's the contents of my main.cpp file: 这是我的main.cpp文件的内容:

#include <string>
#include <iostream>
#include "/root/dev/Practice/include/FileWriter.h"

int main() {
    std::cout << "Hello!" << std::endl;
    FileWriter * fw = new FileWriter("foofile");
    fw->AddLine("CRAP!");
    fw->AddLine("NO!");
    return 0;
}

My FileWriter.h: 我的FileWriter.h:

#ifndef FILEWRITER_H_
#define FILEWRITER_H_

#include <string>
#include <iostream>

class FileWriter{
public:
    FileWriter(std::string);
     ~FileWriter();

    void AddLine(std::string);

private:
    std::string fileLocation;
    std::ofstream  *filestream;
};

#endif /* FILEWRITER_H_ */

...and my FileWriter.cpp: ...以及我的FileWriter.cpp:

#include "/root/dev/Practice/include/FileWriter.h"
#include <fstream>

// g++ linker error if 'inline' not included - why?
inline FileWriter::FileWriter(std::string fileName)
{
    this->fileLocation = fileName;
    const char * x = this->fileLocation.c_str();
    this->filestream = new std::ofstream();
    this->filestream->open(x, std::ios::out | std::ios::app);
}

inline FileWriter::~FileWriter()
{
    this->filestream->close();
}

inline void FileWriter::AddLine(std::string line)
{
    *this->filestream << line << std::endl;
}

This line: 这行:

g++  -c -g -Wall  -o src/main.o src/FileWriter.cpp

should be: 应该:

g++  -c -g -Wall  -o src/main.o src/main.cpp

I don't have access to this compiler, but in the past if you had main() in a C++ file you needed to "decorate" it with __cdecl 我没有访问此编译器的权限,但是在过去,如果您在C ++文件中有main() ,则需要使用__cdecl “修饰”它

int __cdecl main() {

Try that? 试试吧 Or: 要么:

extern "C" int main() {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM