zoukankan
html css js c++ java
【转】解决多文件编译中连接错误 multiple definition of*****
总结了解决multiple definition of
的方法:
问题原因:
当多个文件包含同一个头文件时,并且你的.H里面没有加上条件编译
#ifndef TEST_H
#define TEST_H
#endif
就会独立的解释,然后生成每个文件生成独立的标示符。在编译器连接时,就会将工程中所有的符号整合在一起,由于,文件中有重名变量,于是就出现了重复定义的错误。
方法1:
给每一个头文件加上条件编译,避免该文件被多次引用时被多次解释,这是个应该是习惯。这个方法会解决大部分低级问题。
方法2:
当方法1无效时,可以把所有的全局变量放入一个头文件 global.h (名字随意起,但要加条件编译)中,每一个变量前面加extern,声明一下这些变量将在其它文件中定义。 然后建立一个和头文件名字对应的.c or .cpp文件 如global.c。在里面声明所有的全局变量。例如:void(*Handl_Display)();
然后,让涉及到全局变量的文件include ”global.h“。这样编译时,会先对global.c编译生成一个global.o ,然后再和其它文件的.o链接生成可执行文件。
方法3:
懒人方法,在所有的全局变量前加上static ,声明成静止变量。也能解决问题。
所有的方法都是网来的,O(∩_∩)O哈哈~
谢谢所有的提供方法的哥们~
查看全文
相关阅读:
Python学习之路:MINST实战第一版
Python学习之路:NumPy进阶
Python学习之路:NumPy初识
Python学习之路:一天搞定基础部分
7. 整数反转(leetcode)
1. 两数之和(leetcode)
172. 阶乘后的零(leetcode)
Java模拟斗地主发牌(Collections工具类的应用)
CF刷题-Codeforces Round #481-G. Petya's Exams
【android】安卓平台版本和API版本的对应关系
原文地址:https://www.cnblogs.com/hahawgp/p/2957704.html
最新文章
[洛谷P4467][题解][SCOI2007]k短路
[洛谷P3796][题解][模板]AC自动机
[洛谷P5431][题解][模板]乘法逆元2
[洛谷P1092][题解]虫食算
[洛谷P1967][题解]货车运输
[洛谷P1169][题解][ZJOI2007]午餐
[洛谷P1169][题解][ZJOI2007]棋盘制作
[洛谷P1972][题解][SDOI2009]HH的项链
[洛谷P1373][题解]小a和uim之大逃离
Leetcode WC-107-01 925-长按键入
热门文章
Leetcode WC-108-04 932-漂亮数组
Leetcode WC-108-03 931-下降路径最小和
Leetcode WC-108-02 930-和相同的二元子数组
Leetcode WC-108-01 929-独特的电子邮件地址
PAT-乙级-1034 有理数四则运算
PAT-乙级-1033 旧键盘打字
PAT-乙级-1032 挖掘机技术哪家强
PAT-乙级-1031 查验身份证
PAT-乙级-1030 完美数列
洛谷-P1028 数的计算
Copyright © 2011-2022 走看看
当多个文件包含同一个头文件时,并且你的.H里面没有加上条件编译
#ifndef TEST_H
#define TEST_H
#endif
就会独立的解释,然后生成每个文件生成独立的标示符。在编译器连接时,就会将工程中所有的符号整合在一起,由于,文件中有重名变量,于是就出现了重复定义的错误。
方法1:
给每一个头文件加上条件编译,避免该文件被多次引用时被多次解释,这是个应该是习惯。这个方法会解决大部分低级问题。
方法2:
当方法1无效时,可以把所有的全局变量放入一个头文件 global.h (名字随意起,但要加条件编译)中,每一个变量前面加extern,声明一下这些变量将在其它文件中定义。 然后建立一个和头文件名字对应的.c or .cpp文件 如global.c。在里面声明所有的全局变量。例如:void(*Handl_Display)();
然后,让涉及到全局变量的文件include ”global.h“。这样编译时,会先对global.c编译生成一个global.o ,然后再和其它文件的.o链接生成可执行文件。
方法3:
懒人方法,在所有的全局变量前加上static ,声明成静止变量。也能解决问题。
所有的方法都是网来的,O(∩_∩)O哈哈~
谢谢所有的提供方法的哥们~