开发者

including header file in a separate folder

I created a class (say, myclass.h/cpp). I want to use the class from many different places. Therefore, I put those files in a folder (say, C:\cpp_include) and I want to include them from whatever folder my codes are. I have a code which uses the class (say, main.cpp). In main.cpp, I include myclass:

#include "myclass.h"

I compile using a .pro file and nmake. In the .pro file, I specify the folder as:

INCLUDEPATH += C:\cpp_include

When I compile the code using nmake, myclass.h is properly included, but myclass.cpp doesn't seem to be found by compiler. When I specify myclass.cpp as one of the source files in .pro file:

SOURCES += main.cpp C:\cpp_include\myclass.cpp

The exe file is built correctly. But, I would like myclass.cpp file to be found automatically when myclass.h is开发者_StackOverflow included, i.e. without setting myclass.cpp as a source file. Would this be possible? It looks like that's what happens with classes from Qt and Qwt (e.g .h/cpp files in /src/ folder in Qt and Qwt). Am I missing somthing?

Thanks a lot!

Daisuke


A simple technique is to have build scripts (makefiles) in the cpp directories. Write a rule that traverses the directories, executing the build scripts. This one step in isolating functionality and also allows one to use libraries.


That's just not how it works. The .cpp is the file that matters, header files (.h) just get copied into the other .cpp files. Therefore you need to add the myclass.cpp to your sources for compiling. Or, if it's a library class, you could also compile it once into a static library (.lib) and just add that to your linker files. But you ultimately need to somehow include you implementation in the project where it's used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜