标签: include-guards

添加一个包含保护会破坏构建

我将#ifndef ..#define ..#endif添加到我项目的文件中,编译器失败了.一旦我删除它或在定义中添加任何其他名称它编译好.可能是什么问题呢?

听起来像文件已经声明,但我不知道在哪里.我很好,只是删除它,但我真的想知道为什么会发生这种情况.

error: expected class-name before ‘{’ token
error: ‘QDesignerFormEditorInterface’ has not been declared
Run Code Online (Sandbox Code Playgroud)

还有其他一些错误.

我实际上是在使用Qt的一个例子,"Custom Widget Plugin Example".

不同之处在于我使用自己的类作为自定义小部件(.h,.cpp和.ui文件).

它可能与包含2的文件有关,尽管这就是示例的实现方式.

c++ macros compiler-errors header include-guards

3
推荐指数
1
解决办法
3714
查看次数

#include guard不起作用 - 或者我不明白

我使用几个文件:main.c assembler.c fileHandlers.c handlers.c另外我有几个头文件,包含常量,函数原型等.在其中一个("datatypes.h")我定义了一个字符串数组:

#ifndef DATATYPES_H
#define DATATYPES_H
const char *OPCODES = {"str1",..., str15}.
#endif
Run Code Online (Sandbox Code Playgroud)

然后,我在所有文件中都包含了这个标题(因为它们都在某些时候使用它).

这是我的makefile:

main: main.o assembler.o filesHandler.o handlers.o
    gcc -g -Wall -ansi -pedantic main.o assembler.o filesHandler.o handlers.o -o main

main.o: main.c
    gcc -g -c -Wall -ansi -pedantic main.c -o main.o

assembler.o: assembler.c
    gcc -g -c -Wall -ansi -pedantic assembler.c -o assembler.o

filesHandler.o: filesHandler.c
    gcc -g -c -Wall -ansi -pedantic filesHandler.c -o filesHandler.o

handlers.o: handlers.c
    gcc -g -c -Wall -ansi -pedantic handlers.c -o handlers.o
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,我收到以下错误:

gcc -g …
Run Code Online (Sandbox Code Playgroud)

c c++ header include-guards

3
推荐指数
1
解决办法
523
查看次数

压制gcc警告:"警告:这是前一个定义的位置"

我需要围绕标准系统调用的一组包装器 - 打开,监听等.对于这些我有一些"#define",如:

#define open(a,b,c) JCL_Open(a,b,c)
Run Code Online (Sandbox Code Playgroud)

但是当我编译头文件和相关的.c文件时,我收到以下警告:

/jcl_wrappers.h:114:1:警告:"打开"重新定义
/jcl_wrappers.h:113:1:警告:这是前一个定义的位置

我怎么能抑制这些警告?

linux include-guards gcc-warning

2
推荐指数
2
解决办法
2万
查看次数

包含守卫在名称空间之间是否应该是唯一的?

我在两个名称空间中使用相同的类名,比如A和B.在包含不同命名空间的类时,包含警卫是否应该是唯一的?

我的意思是不能有两个文件名称AFile.h(在不同的目录中)具有相同的包含警卫和声明不同的命名空间?

档案1:

#ifndef AFILE_H

#define AFILE_H

命名空间A {

CAFile类

{...

};

};

#万一

文件2:

#ifndef AFILE_H

#define AFILE_H

命名空间B {

CAFile类

{...

};

};

#万一

c++ namespaces include-guards include

2
推荐指数
2
解决办法
1011
查看次数

如何防止source()R代码被多次包含?

我有很多R源文件.例如,在两个A.RC.R文件中,B.R通过加载source().现在,我想使用的功能均A.RC.R,我怎么能避免采购B.R反复?在C/C++中是否有类似的保护机制?

r include-guards

2
推荐指数
1
解决办法
197
查看次数

为什么头文件中的全局变量会导致链接错误?

我总是得到以下错误,即使我已将include guard放入头文件中.

duplicate symbol _Bittorrent in:
    /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/main.o
    /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/Handshake.o
duplicate symbol _eight_byte in:
    /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/main.o
    /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/Handshake.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

这是.h头文件,.c文件和main.c

main.c中

#include "Handshake.h"
int main(int argc, char** argv)
{
    // some code.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Handshake.h

#ifndef SHT_Handshake_h
#define SHT_Handshake_h

const char    *Bittorrent     =   "BitTorrent protocol";
const char    eight_byte[8]   =   {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

#endif
Run Code Online (Sandbox Code Playgroud)

Handshake.c

#include "Handshake.h"


int …
Run Code Online (Sandbox Code Playgroud)

c include-guards

2
推荐指数
1
解决办法
2771
查看次数

#import是C++ 11标准的一部分吗?

我试图找出在跨平台C++ 11库中使用#import(而不是#include包含保护)是否安全?

我在SO和其他地方发现的文章似乎表明#import被提议包含在C++ 11标准中,但从大约〜2012年的答案判断,它似乎仍然是微软和GCC特定的扩展,导入不是接受标准.

c++ include-guards include c++11

2
推荐指数
1
解决办法
753
查看次数

在.c文件中包含警卫的目的

我一直在C语言的源文件的开头看到这样的代码

#ifndef _INCLUDE_GUARDS_C
#define _INCLUDE_GUARDS_C

main()
{

}

function1()
{
}

#endif

function2()
{
}
Run Code Online (Sandbox Code Playgroud)

我对这个目的感到困惑..?

我知道包含警卫是否在头文件中定义,但是

  1. 这些包括源文件中的警卫的目的是什么?和

  2. 为什么在包含警戒之外定义function2()?

c include-guards header-files c-preprocessor

2
推荐指数
1
解决办法
897
查看次数

你如何定义头文件中的函数?

设置

如果我有这样的程序

一个声明我的主库函数的头文件,primary()并定义了一个简短的简单辅助函数helper().

/* primary_header.h */
#ifndef _PRIMARY_HEADER_H
#define _PRIMARY_HEADER_H

#include <stdio.h>

/* Forward declare the primary workhorse function */
void primary();

/* Also define a helper function */
void helper()
{
    printf("I'm a helper function and I helped!\n");
}
#endif /* _PRIMARY_HEADER_H */
Run Code Online (Sandbox Code Playgroud)

定义它的主要函数的实现文件。

/* primary_impl.c */
#include "primary_header.h"
#include <stdio.h>

/* Define the primary workhorse function */
void primary()
{
    /* do the main work */
    printf("I'm the primary function, I'm doin' work.\n");

    /* …
Run Code Online (Sandbox Code Playgroud)

c program-structure include-guards one-definition-rule

2
推荐指数
2
解决办法
4252
查看次数

使用 #ifdef 时的多个定义

我在编译时遇到问题:Multiple definitions of "myFunction()" 我将在这里大大简化问题。基本上,我有 3 个文件:“main”、“header”和“myLibrary”。

  • 主程序
    #include "header.hpp"

    int main() {  }
Run Code Online (Sandbox Code Playgroud)
  • 头文件.hpp
    #ifndef HEADER_HPP
    #define HEADER_HPP

    #include "myLibrary.hpp"

    // ...

    #endif
Run Code Online (Sandbox Code Playgroud)
  • 头文件.cpp
    #include "header.hpp"

    // ...
Run Code Online (Sandbox Code Playgroud)
  • myLibrary.hpp
    #ifndef LIB_HPP
    #define LIB_HPP

    #if defined(__unix__)
    #include <dlfcn.h>
    std::string myFunction() { return std::string(); }
    #endif

    #endif
Run Code Online (Sandbox Code Playgroud)
  • 我的图书馆.cpp
    #include "myLibrary.hpp"

    //...
Run Code Online (Sandbox Code Playgroud)

那么,为什么编译器说我有呢 Multiple definitions of "myFunction()"

我发现的一条线索:当我获取 header.cpp 并删除显示 的行时#include "header.hpp",程序编译时没有任何抱怨。另一方面,如果我删除myFunction(从 myLibrary.hpp 中),程序也可以毫无抱怨地编译

c++ definition include-guards

2
推荐指数
1
解决办法
2048
查看次数