C++项目布局似乎不正确,编译器找不到实现文件
我正在编写一个 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.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.cpp
中的 cnarray.h
的实现。
我认为这是因为文件夹结构,你能告诉我如何解决这个问题吗?
即使将 src\storage
添加到包含指令中也无法修复它。 (而且我也不想将其添加到包含路径中,因为这对于包来说非常不方便。)
I'm writing a C++ Package for later use using Code::Blocks.
The project structure looks like this:
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\
The 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_
To test the classes, i create a main-function in main.cpp.
// 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();
}
But the compiler (gcc) gives me undefined reference to ...
errors about cnArray
.
Now, the cnarray.cpp
includes cnarray.h
(as it is the implementation file), so using#include "storage/cnarray.cpp"
works just fine.
It seems like the compiler can't find the implementation of cnarray.h
which is located in cnarray.cpp
.
I assume it's because of the folder-structure, can you tell me how I can fix this ?
Even adding src\storage
to the include directives does not fix it. (And I also don't want to add it to the include-paths as this would be very unhandy for a package.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在可以发现错误,
cnArray.h
声明了一个模板类,并且模板类不能在声明之外的另一个文件中实现,因为编译器必须在编译时而不是链接时了解实现。我在互联网上找到了一种解决方法,可以在头文件中
#include
实现,但从编译中排除实现文件。现在效果很好。
干杯
I could spot the error now,
cnArray.h
declared a template class and template classes cannot be implmented in another file than they are declared, because the compiler must know about the implementation when compiling, not when linking.I have found a workaround on the internet to
#include
the implementation in the headerfile, but exclude the implementation file from compiling.This works just fine now.
Cheers
能贴一下你用的编译命令吗?
似乎您只编译了 main.cpp 而没有编译(并因此链接)其他
.cpp
文件。Can you post the compilation command that you use?
Seems like you are compiling only main.cpp and not compiling (and thus linking) the other
.cpp
files.