简体   繁体   English

C 代码在 Windows 中编译,在 Linux 上给出编译错误

[英]C code compiles in Windows, gives compilation error on Linux

Contents of somefile.h: somefile.h 的内容:

#ifndef __SOMEFILE_H
#define __SOMEFILE_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _table_t
{
     void (*somefunction1)();
     void (*somefunction2)(int a);
     void (*somefunction3)(int a, int *b);
}table_t;

void doSomething1();
void doSomething2();

#ifdef __cplusplus
} // error at this line: expected constructor, destructor, or type conversion before '(' token
#endif
#endif

Shown above is the code snippet and the error I get when I compile my code on Linux.上面显示的是代码片段和我在 Linux 上编译代码时遇到的错误。 The same code compiles fine on Windows with no complaints.相同的代码在 Windows 上编译得很好,没有任何抱怨。

About the source file:关于源文件:

all.h is a header file which includes:
#include "header1.h"
#include "header2.h"
#include "header3.h"
#include "somefile.h"

Here is the content of somefile.c这是 somefile.c 的内容

#include "all.h" 
#include "header4.h"
jumptable_t jumptable_a = 
{
       a_function1();
       a_function2(int a);
       a_function3(int a, int *b);
}

//more code
void function1()
{
     a_function1();
}

void function2(int a)
{
    a_function2(a);
}

void function3(int a, int *b)
{
    a_function3(a, b);
}


void doSomething1()
{

}
void doSomething2()
{

}

Macro with leading double underscores is illegal.带有前导双下划线的宏是非法的。 You need to change your include guard.你需要改变你的包含守卫。

You need a ;你需要一个; after the } of jumptable_a .jumptable_a}之后。 And use commas instead of semicolons in the initializer of jumptable_a .并在jumptable_a的初始化程序中使用逗号而不是分号。

The braces make it look like a somethink function-like, but it's not.大括号使它看起来像某种类似于函数的东西,但事实并非如此。

Also, in somefile.h the struct is called table_t , but in somefile.c you are using jumptable_t , which I assume is an error introduced when writing the post here.此外,在somefile.h中,该结构称为table_t ,但在somefile.c中,您使用的是jumptable_t ,我认为这是在此处撰写帖子时引入的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM