由于更改cpp目录而导致链接问题
我是一名java程序员;所以当涉及到 C++ 时,由于链接等问题,我面临很多问题。问题基本上是我想将单个文件(Transformation.cpp)重构为(TrasnformationBackend.cpp)、(TrasnformationFrontend.cpp)和头文件。正如您所看到的,我需要通过所有文件声明一个全局结构,并且我使用头文件中的“extern”来做到这一点,并且它工作得很好。但是,当我尝试将前端 cpp 文件移动到其他目录时,出现未解决的外部错误。
顺便说一句,我正在使用BOOST。
I am a java programmer; so I'm facing a lot of problems when it comes to c++ due to the linking stuff and so on. The problem basically is that I want to re-factor a single file (Transformation.cpp) into (TrasnformationBackend.cpp), (TrasnformationFrontend.cpp) and a header file. As you may see, I needed to declare a global struct through the all the files, and I did that using the "extern" in the header file and it works fine. However, unresolved external error appears when I try to move the frontend cpp file into a different directory.
By the way, I'm using BOOST.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标头中的
extern
声明仅告诉编译器该变量将在编译单元中定义。它实际上并没有创建该变量/为其分配存储空间。如果您的
.h
中有:您需要一个且仅一个
.cpp
文件:否则编译将正常进行,但链接将失败,因为该外部无法解决。
An
extern
declaration in a header only tells the compiler that that variable will be defined in a compilation unit. It does not actually create that variable / allocate storage for it.If you have in your
.h
:You need in one, and only one, of your
.cpp
files:Otherwise compilation will go ok, but link will fail because that external cannot be resolved.
“...当我尝试将前端 cpp 文件移动到其他目录时出现错误。”
好的,如果您查看编译器/IDE 设置,将会有一个设置,它将搜索哪个目录以查找 .cpp 文件;您需要在此处添加新目录吗?
"...error appears when I try to move the frontend cpp file into a different directory."
OK if you look in your compiler/IDE settings there'll be a setting for which directory(ies) it will search through to find .cpp file(s); you need to add the new directory here?