#define和#include编译顺序

时间:2019-06-20 12:22:01

标签: c++ visual-c++ preprocessor

我正在尝试在编译时读取文件。我的代码如下

#define NUM_THREADS 8
static int go=0;
static int exec = 1;
static int ev_od = 0;

static void *
test_thread (void *arg) {
    int j;
    pid_t c, d;
    while(!go) // Wait, so that all threads are here.
        continue;
    // All will fork, hopefully at same time because of go signal wait.
    while(exec) {
        c = fork();
        if (c < 0) {
            printf("SANJAY: fork() failed.\n");
            exit(1);
        } else if (c == 0) { // Child
            exit(0);
        }
        else { // parent
            d = waitpid(c, NULL, 0); 
        }
    }   
    return NULL;
}

extern int __register_atfork(void (*)(void),void (*)(void),void (*)(void),void *); 
static sigset_t s_new;
static sigset_t s_old;
static int count = 0;
static void blocksigprof(void){
    count++;
#ifdef SYS_gettid
    pid_t tid = syscall(SYS_gettid);
    if (tid % 2) {
        printf("sleep.\n");
        usleep(1);
    }
#else
#error "SYS_gettid unavailable on this system"
#endif
    printf("Pre-fork. Count should be one. %d\n", count);
}
static void restoresigprof(void){
    printf("Post-fork. Count should be one. %d\n", count);
    count--;
}
int
main () {
    pthread_t t[NUM_THREADS];
    void *ptr;
    long size = 500 * 1024 * 1024;
    int i, m;
    volatile int result = 0;
    int g_iters = 100;

    (void) __register_atfork(blocksigprof,restoresigprof,NULL,NULL);
    // Increase size, so fork takes time.
    printf("SANJAY: Increasing process size.\n");
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    ptr = malloc(size);
    memset(ptr, 0,  size);
    // Create threads.
    for (i = 0; i < NUM_THREADS; ++i) {
        pthread_create(&t[i], NULL, test_thread, NULL);
    }

    printf("SANJAY: Killing time.\n");
    // Kill time, so that all threads are at same place post it, waiting for go. 100M cycles.
    for (m = 0; m < 1000000; ++m)
        for (i = 0; i < g_iters; ++i )
            result ^= i;
    // Give all threads go at same time.
    printf("SANJAY: Let threads execute.\n");
    go = 1;
    usleep(10000000); // Wait for 10 sec.
    exec = 0;
    // Wait for all threads to finish.
    for (i = 0; i < NUM_THREADS; ++i) {
        pthread_join(t[i], NULL);
    }
    printf("SANJAY: Done.\n");

    return 0;
}

config.txt的内容是

  

ABC

     

DEF

fileContent的期望值是

  

ABC

     

DEF

但是我得到了

  

#include“ config.txt”

用于fileContent。 在Visual c ++中,似乎替换符号的顺序是#define STR(x) #x const char *fileContent = STR( #include "config.txt" ); ,然后是#define。 我可以将顺序更改为#include,然后更改为#include吗? 还是在编译时将文件读取到char *的任何建议?

1 个答案:

答案 0 :(得分:2)

#include是预处理程序指令。

#define也是预处理程序指令。

您不能将一个嵌入另一个。宏参数中的标记不会被解释为进一步的预处理程序指令。这与“顺序”无关,而与嵌套有关。

您也不能只将#include放在字符串文字中,而是there are ways to get what you want

如果the std::embed proposal普及,这在将来的C ++版本中可能会变得更容易。