在 header 中找不到 C++ extern
C++ extern not found when in header
我是 C++ 的新手,我正在努力从一开始就拥有一个好的项目结构。
我正在使用 C 库 libjpeg
并使用
将其包含在我的 .cpp
中
extern "C" {
#include <jpeglib.h>
}
它工作正常,直到我将其删除并放入 header 文件中,现在出现以下错误:
inc/jpeg_utils.h: 6: inc/jpeg_utils.h: extern: not found
inc/jpeg_utils.h: 8: inc/jpeg_utils.h: Syntax error: "}" unexpected
我的headerjpeg_utils.h
:
#ifndef JPEG_UTILS_INCLUDE
#define JPEG_UTILS_INCLUDE
#include <stdio.h>
extern "C" {
#include <jpeglib.h>
}
int read_jpeg_file(char *filename, int decompression);
void write_jpeg_file(char *filename, unsigned char *image_buffer, int image_width, int image_height, int quality);
#endif
并且在 jpeg_utils.cpp
的顶部:
#include "../inc/jpeg_utils.h"
我是否误解了 header 的用法?
如果在 C 文件中包含 jpeg_utils.h
,则 extern "C"
指令将无法编译(显然,C 不是 C++)。
仅当您实际上编译为 C++ 时才向 extern "C"
添加预处理器指令。
#ifdef __cplusplus
extern "C" {
#endif
#include <jpeglib.h>
#ifdef __cplusplus
}
#endif
我是 C++ 的新手,我正在努力从一开始就拥有一个好的项目结构。
我正在使用 C 库 libjpeg
并使用
.cpp
中
extern "C" {
#include <jpeglib.h>
}
它工作正常,直到我将其删除并放入 header 文件中,现在出现以下错误:
inc/jpeg_utils.h: 6: inc/jpeg_utils.h: extern: not found
inc/jpeg_utils.h: 8: inc/jpeg_utils.h: Syntax error: "}" unexpected
我的headerjpeg_utils.h
:
#ifndef JPEG_UTILS_INCLUDE
#define JPEG_UTILS_INCLUDE
#include <stdio.h>
extern "C" {
#include <jpeglib.h>
}
int read_jpeg_file(char *filename, int decompression);
void write_jpeg_file(char *filename, unsigned char *image_buffer, int image_width, int image_height, int quality);
#endif
并且在 jpeg_utils.cpp
的顶部:
#include "../inc/jpeg_utils.h"
我是否误解了 header 的用法?
如果在 C 文件中包含 jpeg_utils.h
,则 extern "C"
指令将无法编译(显然,C 不是 C++)。
仅当您实际上编译为 C++ 时才向 extern "C"
添加预处理器指令。
#ifdef __cplusplus
extern "C" {
#endif
#include <jpeglib.h>
#ifdef __cplusplus
}
#endif