无法使用 microsoft Visual Studio c++ 编译 c 代码2008年速成版

发布于 12-06 02:09 字数 457 浏览 0 评论 0原文

我有可以在 Linux 中使用 gcc 编译的 ac 代码。但是当我尝试使用 microsoft Visual Studio C++ 2008 Express 版本使用 ide 编译它时,它显示了

vec.obj : error LNK2005: _INIT_SETA already defined in a.obj
fatal error LNK1169: one or more multiply defined symbols found

我检查头文件的错误,并且所有这些文件都有预处理器保护以防止多次包含头文件,例如

#ifndef _vec_h_
#define _vec_h_

然后我尝试在Visual Studio命令提示符下编译它,

cl main.c

它可以编译。问题是什么?

I have a c codes which can be compiled in Linux using gcc. But when I try to compile it using microsoft visual studio c++ 2008 express edition, using the ide, it shows the error

vec.obj : error LNK2005: _INIT_SETA already defined in a.obj
fatal error LNK1169: one or more multiply defined symbols found

I checked the header files, and all of them have the preprocessor guard to prevent the header to be included multiple times, e.g.

#ifndef _vec_h_
#define _vec_h_

Then I tried compiling it in visual studio command prompt,

cl main.c

It can be compiled. What is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇2024-12-13 02:09:26

我检查了头文件,它们都有预处理器保护,以防止头文件被多次包含
这只会阻止预处理器在单个编译单元(cpp 文件)中多次包含单个头文件。因此,两个 cpp 文件中仍然包含该标头,并且该标头定义了 _INIT_SETA 对象。如果标头仅包含声明而不包含定义,则可以避免该问题。 (没有函数代码,也没有全局变量。)

Hpp 文件:

#ifndef _vec_h_
#define _vec_h_
class vector {
    function();  //function prototype.  No definition
};  //class declaration.  No instantiation
extern vector myvector;   //variable declaration.  No instantiation
#endif //_vec_h_

Cpp 文件:

#include "vec.h"

vector::function() {} //function definition only in this cpp file

vector myvector; //variable instantiation only in this cpp file

唯一的例外通常是模板,它完全位于头文件中,链接器自行计算出来。

I checked the header files, and all of them have the preprocessor guard to prevent the header to be included multiple times
This only prevents the preprocessor from including a single header file multiple times in a single compilation unit (cpp file). So you still have that header included in both cpp files, and that header defines a _INIT_SETA object. The problem is avoided if headers contain only declarations, and not definitions. (No function-code, and no global variables.)

Hpp file:

#ifndef _vec_h_
#define _vec_h_
class vector {
    function();  //function prototype.  No definition
};  //class declaration.  No instantiation
extern vector myvector;   //variable declaration.  No instantiation
#endif //_vec_h_

Cpp file:

#include "vec.h"

vector::function() {} //function definition only in this cpp file

vector myvector; //variable instantiation only in this cpp file

The only exceptions are usually templates, which goes entirely in the header file, and the linker figures it out itself.

梦途2024-12-13 02:09:26

您发布的错误表明存在 vec.cac (假设您没有尝试链接预先存在的目标文件),它们都定义 <代码>INIT_SETA。这是链接器错误,而不是编译错误。

cl main.c 仅将文件编译为目标文件,没有进行链接。如果您尝试从命令行使用 (link.exe) 将所有目标文件链接在一起,您仍然会收到相同的错误。在错误中列出的两个文件中搜索 INIT_SETA 符号的多个定义。

一种可能的解决方案是在使用它的两个文件之一中声明它 extern ,然后这两个文件将共享同一个实例。

如果两个文件都需要有私有副本,则应取出头文件中出现的所有 extern INIT_SETA 声明(并将 static 添加到每个源文件的定义中)。

The error you posted indicates the existence of a vec.c and a.c (assuming you're not trying to link in pre-existing object files), both of which define INIT_SETA. This is a linker error, not a compilation error.

cl main.c only compiles the file to an object file, there is no linking going on. If you try to link all your object files together using (link.exe) from the command line, you'll still get the same error. Search the two files listed in the error for multiple definitions of the INIT_SETA symbol.

One possible solution could be to declare it extern in one of the two files using it, then the two files would share the same instance.

If both files need to have private copies, you should take out any extern INIT_SETA declarations appearing in the header files (and add static to the definitions in each source file).

芸娘子的小脾气2024-12-13 02:09:26

“找到一个或多个多重定义符号”是链接器错误而不是编译器错误。当两个或多个目标文件包含同一符号的定义时,就会发生这种情况。在这种情况下,vec.obja.obj 都以某种方式包含 _INIT_SETA 符号的条目,因此您需要弄清楚如何vec.obja.obj 的源代码各自将 _INIT_SETA 符号引入到各自的翻译单元(编译)中。

请注意,_INIT_SETA 是编译器为 C 标识符 INIT_SETA 生成的符号。也许 INIT_SETA 的定义是通过宏扩展内联的?在这种情况下,INIT_SETA 的声明可能需要声明为static

“多个符号”问题的存在不会影响源文件的编译;相反,链接步骤将会失败,因为链接器不知道要链接到哪个_INIT_SETA条目。

"one or more multiply defined symbols found" is a linker error rather than a compiler error. It occurs when two or more object files contain a definition for the same symbol. In this case, both vec.obj and a.obj somehow both contain an entry for the _INIT_SETA symbol, so you need to figure out how the sources for vec.obj and a.obj each introduce an _INIT_SETA symbol into their respective translation units (compilations).

Note that _INIT_SETA is the compiler-generated symbol for the C identifier INIT_SETA. Perhaps a definition of INIT_SETA was inlined via macro expansion? In this case, the declaration of INIT_SETA likely needs to be declared static.

The presence of a "multiple symbol" issue does not affect compilation of the source files; rather the linking step will fail because the linker does not know which _INIT_SETA entry to link with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文