![](/img/trans.png)
[英]C++ where I can find the implementation files of the standard library for the gcc compiler?
[英]C++ Project Layout seems incorrect, compiler can't find implementation files
我正在編寫一個C ++程序包,供以后使用Code :: Blocks使用。
項目結構如下所示:
cNormal\
cNormal.cdp
src\
main.cpp # for testing purpose
cnormal_defs.h # important compiler definitions
cnormal_storage.h # includes all .h files from "./storage"
storage\
cnarray.h
cnarray.cpp
cnstack.h
cnstack.cpp
bin\
obj\
cnormal_storage.h:
// cnormal_storage.h
// *****************************************************************
// Includes all necessary headers for the cNormal storage subpackge.
//
#ifndef _cNORMAL_STORAGE_H_
#define _cNORMAL_STORAGE_H_
#include "storage/cnarray.h"
#include "storage/cnstack.h"
#endif // _cNORMAL_STORAGE_H_
為了測試這些類,我在main.cpp中創建了一個main函數。
// main.cpp
// *****************************************************************
// The main-file.
//
#include <iostream>
#include "cnormal_storage.h"
using namespace std;
int main() {
cnArry<int> arr(10);
arr[9] = 999;
arr[0] = 0;
cout << arr[9] << endl;
cout << arr.getLength();
}
但是編譯器(gcc)給了我關於cnArray
錯誤的undefined reference to ...
。
現在, cnarray.cpp
包含cnarray.h
(因為它是實現文件),因此使用
#include "storage/cnarray.cpp"
可以正常工作。
似乎編譯器找不到cnarray.h
中的cnarray.cpp
。
我認為這是由於文件夾結構所致,您能告訴我如何解決此問題嗎?
即使將src\\storage
添加到include偽指令也無法修復它。 (而且我也不想將其添加到include-paths中,因為這對於軟件包來說非常不便。)
您可以發布使用的編譯命令嗎?
似乎您僅在編譯main.cpp而不在編譯(因此鏈接)其他.cpp
文件。
我現在可以發現該錯誤, cnArray.h
聲明了一個模板類,並且模板類不能cnArray.h
比聲明它們還要多的文件中,因為編譯器必須在編譯時(而不是在鏈接時)知道實現。
我發現在互聯網上的一種變通方法,以#include
在 headerfile實施,而是從編制中排除的實現文件。 現在工作正常。
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.