Extern "C"错误在'int'之前'asm'或'__attribute__'

Extern "C" error expected '=', ',', ';', 'asm' or '__attribute__' before 'int'

本文关键字:asm 之前 attribute int Extern 错误      更新时间:2023-10-16

我正在尝试将编译库包含在北欧nrf52840上的C项目中。下面(据我了解(是一种链接到项目其余部分.lib文件中foobar的一些方法的方法。尝试使用Segger Embedded Studio编译它时,我收到以下expected '=', ',', ';', 'asm' or '__attribute__' before 'int'错误,其中包含以下代码片段:

#ifndef _FOOBAR_SERVICE_H_
#define _FOOBAR_SERVICE_H_
#if (defined(__linux__) || defined(__APPLE__) || defined(ARDUINO) || 
defined(__MSP430FR5969__))
#define IMPORT __attribute__ ((visibility ("default")))
#define EXPORT __attribute__ ((visibility ("default")))
#define LOCAL  __attribute__ ((visibility ("hidden")))
#elif defined(_WIN32)
#define EXPORT __declspec(dllexport)
#endif
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
EXPORT int ble_foo(unsigned char  *a, unsigned char *buffer);  //<--(error)
EXPORT int ble_bar(unsigned char  *b, unsigned char *buffer);  //<--(same error)
#ifdef __cplusplus
}
#endif
#endif /* _FOOBAR_SERVICE_H_ */

以上内容#include "foobar_ble.h"包含在我的main.c文件中。

部分原因可能是我对extern "C"的误解,我认为这是编译 C 代码的一种方式。我相信#ifdef __cplusplus正在检查编译为 c++ 所以这是否意味着extern "C"甚至没有在 C 环境中使用?

另外,我似乎无法在 C 中找到对EXPORT关键字的良好解释。这也可能是我问题的根源。

Tl;大卫:太愚蠢了,问题太多了,需要帮助。请和谢谢。

我认为问题是您情况下的EXPORT没有定义为任何东西。因此,它只会保留在源代码中并导致语法错误。它可能打算在静态库上定义为类似 __declspec(dllexport( 的内容,而对于静态使用,则不定义为任何内容(空字符串(。

您应该能够通过将EXPORT定义为空字符串来修复它。根据库的不同,在某些配置头文件中可能会有一个位置。否则,您也可以在编译器调用级别上使用 define ,但这可能不是可取的,因为它会从源代码中删除所有EXPORT单词。