C ++中的多个定义错误 [英] Multiple definitions error in C++

查看:227
本文介绍了C ++中的多个定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C ++程序,其中每个文件都有自己的一组全局变量声明。大多数这些文件使用在其他文件中使用extern定义的全局变量。

I'm writing a C++ program where each file has it's own set of global variable declarations. Most of these files make use of global variables that were defined in the other files using extern.

以下是类似我的程序的示例:

Here's an example similar to my program:

Main.cpp p>

Main.cpp

#include "stdafx.h"
#include <iostream>
#include "Other_File.cpp"

int var1;
int var2;

int main()
{
    var1 = 1;
    var2 = 2;
    otherFunction();
    var4 = 4; // From Other_File.cpp

    std::cout << var1 << " " << var2 << " " << var3 << " " << var4 << std::endl;

    return(0);
}

Other_File.cpp
$ b

Other_File.cpp

extern int var1;
extern int var2;

int var3;
int var4;

void otherFunction()
{
     var3 = var1 + var2;
     var4 = 0;
}



当在Visual Studio(Windows)中构建此代码时,输出正确。但是当我尝试在Linux上使用g ++时,我收到以下错误:

When I build this code in Visual Studio (Windows), everything runs fine and the output is correct. But when I attempt to build using g++ on Linux I receive the following error:


g ++ -o Testing Testing.o Other_File.o Other_File。 o:(bss + 0x0):
Other_File.o :(。bss + 0x4):多个定义 var4'
Testing.o :( .bss + 0x4):首先定义这里Other_File.o:在函数
otherFunction ()':Other_File.cpp :(。text + 0x0):
的多重定义
otherFunction()'Testing.o:Testing.cpp :(。
here collect2:ld returned 1 exit status make:*** [Testing] Error 1

g++ -o Testing Testing.o Other_File.o Other_File.o:(.bss+0x0): multiple definition of var3' Testing.o:(.bss+0x0): first defined here Other_File.o:(.bss+0x4): multiple definition ofvar4' Testing.o:(.bss+0x4): first defined here Other_File.o: In function otherFunction()': Other_File.cpp:(.text+0x0): multiple definition of otherFunction()' Testing.o:Testing.cpp:(.text+0x0): first defined here collect2: ld returned 1 exit status make: *** [Testing] Error 1

因为我在主文件中包含了其他文件?

如果没有,我的代码有什么问题?

If not what's the issue with my code?

编辑:这是我的Makefile for g ++的内容:

This is the content of my Makefile for g++:

Testing: Testing.o Other_File.o
    g++ -o Testing Testing.o Other_File.o

Testing.o: Testing.cpp
    g++ -c -std=c++0x Testing.cpp

Other_File.o: Other_File.cpp
    g++ -c -std=c++0x Other_File.cpp

clean:
    rm *.o Calculator


推荐答案

不要< c $ c> #include 将源文件复制到另一个源文件中。

Don't #include a source file into another source file. There times and places when that is okay, but only in like less than 0.001% of all programs is that needed.

你应该做的是创建一个标题

What you should do is create a header file which contains declarations of the things needed in both source files.

然后您的代码将如下所示:

Then your code would look like this:


  1. main.cpp 源文件

#include "stdafx.h"
#include <iostream>
#include "Other_File.h"  // Note inclusion of header file here

int var1;
int var2;

int main()
{
    var1 = 1;
    var2 = 2;
    otherFunction();
    var4 = 4; // From Other_File.cpp

    std::cout << var1 << " " << var2 << " " << var3 << " " << var4 << std::endl;
}


  • other_file.cpp

  • other_file.cpp source file, just like you have it now

    other_file.h 头文件新文件

    #pragma once
    
    // Declare the variables, so the compiler knows they exist somewhere
    extern int var3;
    extern int var4;
    
    // Forward declaration of the function prototype
    void otherFunction();
    


  • 链接在一起,形成最终的可执行文件。此链接步骤是您的构建失败的位置。它会注意到在 other_source.cpp 中定义的变量在从该源文件创建的对象文件中定义,但是由于它包含在 main .cpp 源文件,然后从该源文件创建的对象文件。

    Both source files would then be compiled separately and linked together to form the final executable. This linking step is where your build fails. It will notice that the variables defined in other_source.cpp are defined in the object file created from that source file, but since you include it into the main.cpp source file then the object file created from that source file as well.

    这是为什么你需要了解 翻译单位 ,这是编译器实际看到的。 C ++源文件经历了许多翻译阶段,每个阶段都有自己的特殊部分。

    This is why you need to learn about translation units, which is what the compiler actually see. A C++ source file goes through many phases of translation, each one doing its own special part. Roughly a translation unit is a single source file with all the headers included.

    这是一个很好的理由来了解预处理器 #include 指令。它基本上将包含的文件插入到正在预处理的源文件中。其中 #include 指令是,在预处理之后,它将是包含的文件的内容,这是编译器将看到的。

    This is also a good reason to learn what the preprocessor #include directive does. It basically inserts the included file, as is, into the source file being preprocessed. Where the #include directive was, after preprocessing it will be the contents of the included file, and that is what the compiler will see.

    这篇关于C ++中的多个定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆