2017-05-09 37 views
1

我對C++非常陌生,我試圖盡力從一開始就擁有一個好的項目結構。 我現在用的是C庫libjpeg,幷包括在我的.cpp與使用在標題中找不到C++ extern

extern "C" { 
    #include <jpeglib.h> 
} 

它工作得很好,直到我刪除它放在一個頭文件,它現在給了我以下錯誤:

inc/jpeg_utils.h: 6: inc/jpeg_utils.h: extern: not found 
inc/jpeg_utils.h: 8: inc/jpeg_utils.h: Syntax error: "}" unexpected 

我的頭jpeg_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 

而且在頂部:

#include "../inc/jpeg_utils.h" 

我誤解了標題的使用嗎?

+0

是否將標題重命名爲jpeg_utils.hpp會改變什麼? – Jovasa

+1

@Jovasa爲什麼要改變文件的名稱,有什麼區別? –

+0

不幸的不是。 – SarahHime

回答

2

如果在C文件中包含jpeg_utils.h,那麼extern "C"指令將不會編譯(顯然,C不是C++)。

僅在實際上將您編譯爲C++時才向extern "C"添加預處理器指令。

#ifdef __cplusplus 
extern "C" { 
#endif 

    #include <jpeglib.h> 

#ifdef __cplusplus 
} 
#endif 
+0

我的項目是用C++語言編寫的,只有我需要的jpeg庫是用C語言編寫的。我用'.h'而不是'.hpp'可能會引起混淆,但是我知道沒有區別。所以不幸的是,這個解決方案無法解決我的問題 – SarahHime

+0

@SarahHime - 沒有任何關於後綴的混淆。這個錯誤表明你將你的頭文件包含在一個編譯爲純C的文件中。事實上,在決定它不能解決問題之前,你是否嘗試了這個解決方案? – StoryTeller

+0

當然我試過了,我仍然得到相同的錯誤(我正在用g ++編譯)。 – SarahHime