标签: preprocessor-directive

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

关于缺少预处理器符号的编译器警告

背景

我有一个带有配置头文件的C项目,例如:

// config.h
#define FEATURE_1_AVAILABLE      1
#define FEATURE_2_AVAILABLE      0
#define FEATURE_3_AVAILABLE      1
Run Code Online (Sandbox Code Playgroud)

模块使用这些标志在构建中包括/排除功能:

#include "config.h"
void foo(void) {
    ...
    #if FEATURE_1_AVAILABLE
    useFeature1();
    #endif
    ...
Run Code Online (Sandbox Code Playgroud)

有不同版本的config.h不同版本。

问题

#include "config.h" 被意外删除,因此即使在config中启用了该功能,该功能也未处于活动状态。

不幸的是,它会#if FEATURE_1_AVAILABLE默默地评估0未定义的时间。因此,如果未定义,我需要一个错误。

选项1

我可以在每个条件上添加额外的“如果定义”测试。

#ifndef FEATURE_1_AVAILABLE
    #error No feature 1  
#elif FEATURE_1_AVAILABLE
    useFeature1();
#endif
Run Code Online (Sandbox Code Playgroud)

缺点是一个功能标记可能会在模块中的许多不同位置进行测试,并且添加多条预处理程序行不会使代码更漂亮。

选项2

在模块的开头添加单个测试:

#ifndef FEATURE_1_AVAILABLE
    #error  No feature 1  
#endif
Run Code Online (Sandbox Code Playgroud)

但这可能会因为相同的原因而丢失/遗忘#include "config.h"

选项3

将标志定义为宏:

#define FEATURE_1_AVAILABLE()      1
Run Code Online (Sandbox Code Playgroud)

并用作:

#if FEATURE_1_AVAILABLE()
Run Code Online (Sandbox Code Playgroud)

不幸的是,如果您忘记括号,这也将无声地失败:

#if FEATURE_1_AVAILABLE
Run Code Online (Sandbox Code Playgroud)

选项X …

c c-preprocessor preprocessor-directive

4
推荐指数
1
解决办法
1106
查看次数

将#elif与#ifdef一起使用是否合法?

一个简单的问题,谷歌没有帮助我.在C++中使用#elif上下文中的子句是否合法#ifdef?它似乎与c ++ 11模式(MSVC 2015/2017,clang,GCC)中的所有主要编译器一样编译和工作,但我不确定它是否符合标准.

c++ preprocessor c-preprocessor preprocessor-directive

4
推荐指数
3
解决办法
2713
查看次数

预处理器:获取操作系统.Net Core

我正在编写一个我希望在 Windows 和 Linux 上使用的类。

此类中的方法之一是访问Windows 注册表。

我希望实现的是在使用 Linux 计算机时以某种方式禁用此特定方法。

首先,我做了一些研究,看看.Net Core是否有一些东西可以让我检查正在使用哪个操作系统,我发现了这个并且确实可以工作。

当我在访问方法时将其实现到代码中时,我希望禁用访问 Windows 注册表的方法,但是我最接近的方法是使用 switch 语句,如下所示

switch (OS)
{
    case OSX:
    return;

    case LINUX:
    return
}
Run Code Online (Sandbox Code Playgroud)

如果return操作系统不受支持,这可行,但是我随后认为禁用它一起访问会比为不支持该特定方法的操作系统抛出错误要好得多,

然后我继续查看预处理器指令认为,如果我能够检测并禁用取决于框架等的部分代码,也许我可以使用类似的东西来根据操作系统禁用部分代码,这样即使在尝试访问时也永远不会调用它们我从那里继续的方法

是看看我是否可以使用禁用部分代码preprocessor directives
我找到了这个

我知道它是针对C++的,但它似乎是我能找到的最接近我想要实现的目标的在.Net Core

一个完美的世界中,它看起来像这样

    /// <summary>
    /// Get the file mime type
    /// </summary>
    /// <param name="filePathLocation">file path location</param>
    /// <returns></returns>
    `#if WINDOWS`
    public static string GetMimeType(this string filePathLocation)
    {
        if (filePathLocation.IsValidFilePath())
        { …
Run Code Online (Sandbox Code Playgroud)

c# preprocessor-directive .net-core

4
推荐指数
1
解决办法
3729
查看次数

在 C 中调用函数时 \ 是什么意思?

我正在查看一些代码,发现它在调用函数时使用 \ 来分隔行,这是否意味着什么?还是只是为了更具可读性?

        function(\
        lets_say_this_a_long_attribute, \
        and_this_is_another_attribute_with_a_long_name_or_operations, \
        attribute);
Run Code Online (Sandbox Code Playgroud)

c preprocessor preprocessor-directive

4
推荐指数
1
解决办法
104
查看次数

这些编译器指令有什么区别?

这些指令之间的区别是什么?

#ifdef FOO

#if defined FOO

#if defined(FOO)
Run Code Online (Sandbox Code Playgroud)

我正在使用CCS编译器,但我也对其他C编译器感兴趣.

c c-preprocessor preprocessor-directive

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

#pragma pack(16)和#pragma pack(8)的效果总是一样吗?

我试图通过使用对齐数据成员#pragma pack (n).以下面的例子为例:

#include <iostream>
using namespace std;

#pragma pack(8) // or (16)

struct A
{
    int a;
    char b;
    char c;
    char d;
    char e;
    char f;
    double g;
};
int main()
{
    cout << sizeof(A) << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

两者都打印24#pragma pack(8)#pragma pack(16).根据我的理解,我可以理解n=8数据对齐的结果如下:

Bytes: |1 2 3 4|5|6|7|8|9|10 11 12 13 14 15 16|17 18 19 20 21 22 23 24|
 Data: |a      |b|c|d|e|f|padding             |g                      |
Run Code Online (Sandbox Code Playgroud)

但我无法理解为什么结果仍然24 …

c++ pragma memory-alignment c-preprocessor preprocessor-directive

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

VS2013忽略编译指示警告禁用

int main() {
#pragma warning(push)
#pragma warning(disable: 4101)
    int i;
#pragma warning(pop)
}
Run Code Online (Sandbox Code Playgroud)

########################### 要么 ###################### #####

int main() {
#pragma warning(suppress: 4101)
    int i;
}
Run Code Online (Sandbox Code Playgroud)

我相信其中任何一个都应该在Visual Studio 2013上进行编译,警告被视为错误,没有C4101警告(关于我是一个未引用的局部变量.)这是一个4级警告.

但是,它仍在给我警告.即使我关闭被视为错误的警告,它仍然会给我警告,尽管它编译因为没有错误.

我没有使用预编译的头文件/ stdafx.h.我看到其他一些问题,说这个pragma技术可以解决实际代码(也许它们意味着代码在头文件中,但没有指定或看起来像那样).其他一些人在stdafx.h被忽略之前含糊地说没有解释任何#pragma命令.但是' https://msdn.microsoft.com/en-us/library/d9x1s805(v=vs.120).aspx '特别提到了pragma可以在源代码中覆盖编译器选项.当然,我从来没有使用stdafx.h,更像是一个unix家伙,所以也许有一些完全基本的东西我完全不知道了.

我已经尝试过指定C4101而不仅仅是4101,并且它无法在那里处理alpha.我已经尝试指定#pragma warning(push 2),即使这会触及我不想做的所有3级和4级警告 - 甚至那仍然会给出4级警告.

这是我的编译器命令行选项: /MP /GS /W4 /Zc:wchar_t /Zi /Gm- /Od /sdl /Fd"<...>\pragmaWarning\Intermediate\vc120.pdb" /fp:precise /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /Fa"<...>\pragmaWarning\Intermediate\" /EHsc /nologo /Fo"<...>\pragmaWarning\Intermediate\" /Fp"<...>\pragmaWarning\Intermediate\pragmaWarning.pch"

如您所见,这些命令行选项省略了被视为错误的警告.

c++ pragma c-preprocessor preprocessor-directive visual-studio-2013

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

编译器如何从头文件中知道源文件存在于某处?

我刚开始学习C/C++,但我有点困惑.我经常看到#include预处理器指令,带有头文件参数:

#include <stdio.h>
#include <iostream.h> 
#include "windows.h"
#include <math.h>
Run Code Online (Sandbox Code Playgroud)

但有时.h缺少:

#include <iostream>
Run Code Online (Sandbox Code Playgroud)

我已经知道,头文件只包含函数的签名和一些预处理器命令.但是编译器何时以及如何包含数学函数,如果我包括math.h

如果我创建一个库,那么放置.c/.cpp/.h文件的位置,以及如何包含它们?

c c++ include preprocessor-directive

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

当我尝试编译这个程序时,我在程序错误中得到了'#'

当我尝试使用-fopenmpflag 编译时,我收到以下错误:

流浪#于节目中

以下是我的代码:

#include<omp.h> 
int main()

{        #pragma omp parallel 
     {
     int id=0;
     printf("hello(%d) ",id);
     printf("world(%d)\n",id);
     }
}
Run Code Online (Sandbox Code Playgroud)

c parallel-processing pragma openmp preprocessor-directive

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