collect2:ld返回1退出状态错误

时间:2013-02-01 18:16:09

标签: c++

我有三个文件如下:

Movie.h

struct Movie{
    char title[30];         // the hour of the current time
    char director[30];          // the minute of the current time
    int length;
} ;

void printMovieInfo(Movie *s);

Movie.cpp

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include "Movie.h"

using namespace std;

void printMovieInfo(Movie *s){

    cout << "Hi";

}

以及主

的文件
#include "Movie.cpp"
using namespace std;

int main()
{
    struct Movie *m;
    printMovieInfo(m);
}

运行程序时,出现以下错误:

    collect2 ld returned 1 exit status

和警告:

/tmp/ccIe4dlt.o In function `printMovieInfo()':
Movie.cpp (.text+0x0): multiple definition of `printMovieInfo()'
/tmp/cc91xrNB.o HelloWorld.cpp:(.text+0x0): first defined here

我只是想调用一个函数来打印“嗨”,但我不确定为什么会出现这个错误

1 个答案:

答案 0 :(得分:2)

请勿使用您想要#include "movie.cpp"的{​​{1}}!

包含“.cpp”文件是非常罕见的 - 它们被编译为单独的单元,然后由链接器链接在一起(此处称为#include "movie.h")。