我使用VS並在C中使用我的第一部分可重用代碼,所以我將代碼分離爲頭文件和源文件,一些意外的行爲。
在我看來,不知何故,鏈接器無法解析我的typedefed結構。我嘗試將實際的結構聲明移動到實現文件中,並從頭文件中敲入結構,但沒有成功。
爲了簡明起見,我刪除了所有的實現,因爲這不是問題。Visual Studio C編譯器未將頭文件鏈接到其源代碼對象
// queue.h
#ifndef INC_QUEUE_
#define INC_QUEUE_
#include <stdint.h>
// STRUCT TYPEDEF
typedef struct QueueElement { ... } QueueElement;
typedef struct Queue { ... } Queue;
// FUNCTION PROTOTYPES
Queue* queue_construct();
uint32_t queue_peek(Queue *);
void queue_enqueue(Queue *, uint32_t);
uint32_t queue_dequeue(Queue *);
uint8_t queue_empty(Queue *);
void queue_destroy(Queue *);
#endif
// queue.c
#include <stdlib.h>
#include "queue.h"
Queue* queue_construct() { ... }
uint32_t queue_peek(Queue *queue) { ... }
void queue_enqueue(Queue *queue, uint32_t element) { ... }
uint32_t queue_dequeue(Queue *queue) { ... }
uint8_t queue_empty(Queue *queue) { ... }
void queue_destroy(Queue *queue) { ... }
的源代碼是所有細,這一切都編譯,但是當我試圖包括在此例如這個文件......
#include <stdio.h>
#include "queue.h"
int main()
{
Queue *q = queue_construct();
queue_destroy(q);
return 1;
}
錯誤我接頭堅持給我:
error LNK2019: unresolved external symbol "struct Queue * __cdecl queue_construct(void)" ([email protected]@[email protected]@XZ) referenced in function _main
error LNK2019: unresolved external symbol "void __cdecl queue_destroy(struct Queue *)" ([email protected]@[email protected]@@Z) referenced in function _main
任何幫助,將不勝感激,這可能是一個愚蠢的問題,但正如我剛纔所說,我已經從來沒有在VS C.攻克了「大/多文件」項目
編輯
有人向我指出的是包含主函數的源文件確實是一段cpp的源內文件,但我的頭文件和實現都在C文件中。據我所知,你可以在你的C++代碼中的任何地方使用C文件,其中一個例子就是能夠使用C++中的任何C系統庫。
該程序現在工作正常,但現在我的問題是:爲什麼我不能在cpp文件中使用C頭?爲什麼這裏是這種情況。顯然我錯過了一些重要的東西。
它看起來像queue.c中的函數無法找到。你確定這個文件是你的項目的一部分,並與main.c一起構建? – 2015-04-03 16:02:41
我會這麼說。我有一個VS爲一個給定的C++項目創建的默認文件夾,我的* .h文件在「頭文件」文件夾中,源文件在「源文件」文件夾中。 – Pavlin 2015-04-03 16:21:44
我看到的唯一情況是struct name和typedef是相同的。嘗試更改'typedef結構隊列{...}隊列;'到'typedef結構隊列_ {....}隊列;'我很難相信這會是原因,但它是唯一的我可以看到。這假定main.c和queue.c都被編譯並鏈接在一起。注:早在上個世紀(大約1983年),當我學習時,有人告訴我確保名稱不同......不記得解釋,我只是總是這樣做。 – thurizas 2015-04-03 16:44:39