简体   繁体   中英

How to include header files of another projects in the same solution?

I have two C++ projects (proj1 and proj2) in the same solution. How to include the header files from proj1 into proj2? I included those header files in proj2 by setting Additional Include Directories , but cannot link those functions - Error: unresolved externals .

你不需要从porj2的引用,添加proj1,然后编译器将在编译proj2时链接proj1。

So your problem is not the actual inclusion of the header files, it's linking to the object files of the other project.

There are basically two solutions to that problem:

  1. Simply pull in the source file from other project into the project where they are needed. This will cause you to build the source files twice, once for each project. Note that I don't mean you should physically copy the files on disk, just drag and drop the source files in the solution side-bar.

  2. Put common code in a third project, as a library. Then both your projects uses this library, and links with it.

I highly recommend the second solution.

While you are getting linking error that means you have compiled your code successfully and that means that you have included your headers correctly. Congrats! However, you are facing unresolved externals error. This is because you have just tell the compiler where to find the .h file but what about the real code? yes the one in the .cpp ?

Options:

  1. Put all your code in .h files which could be a fast solution but not an good idea (unless you need portable solution for templated code).
  2. Build your first project as static library and link with the output .lib file (you could find how to do it by quick googling)
  3. Bring your sources ( .h and .cpp ) to the other project and build them inside your project (Huge redundancy happened here).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM