简体   繁体   English

Visual Studio 包含来自其他项目的 cpp 文件

[英]Visual Studio Include cpp file from other project

I have a C++ .h and .cpp file from another project that I want to include into my project.我有一个来自另一个项目的 C++ .h 和 .cpp 文件,我想将其包含到我的项目中。 I don't want to copy the files over into my project since I want any changes to those files be applied to both projects.我不想将文件复制到我的项目中,因为我希望对这些文件的任何更改都应用于两个项目。

I've included the directory of the file's folder in the我已将文件文件夹的目录包含在

Properties->VC++ Directories->Include Directories属性->VC++ 目录->包含目录

I've also included the folder in the我还将文件夹包含在

Properties->C/C++ -> General -> Additional Include Directories属性->C/C++ -> 常规 -> 附加包含目录

The .h files seem to work. .h 文件似乎有效。 If I rename the include to anything other than如果我将包含重命名为除

#include "myfile.h" #include "myfile.h"

The cpp file gets unknown definitions. cpp 文件获得未知定义。

When I compile.当我编译。 The error is错误是

fatal error C1083: Cannot open source file: '..\\..\\..\\..\\..\\..\\my project\\myfile.cpp': No such file or directory致命错误 C1083:无法打开源文件:“..\\..\\..\\..\\..\\..\\my project\\myfile.cpp”:没有这样的文件或目录

If I remove the cpp file from the project.如果我从项目中删除 cpp 文件。 Then I get a long list of unresolved functions.然后我得到一长串未解析的函数。

error LNK2019: unresolved external symbol "public: unsigned long __thiscall myclass::myfunction"错误 LNK2019:未解析的外部符号“public: unsigned long __thiscall myclass::myfunction”

How can I include both the .h and .cpp file into my second project?如何将 .h 和 .cpp 文件同时包含到我的第二个项目中?

For cpp files you can just use right mouse click on project, select "add"->existing item.对于 cpp 文件,您可以使用鼠标右键单击项目,选择“添加”-> 现有项目。 Then they should compile with others, when a build initiated.然后,当构建启动时,他们应该与其他人一起编译。

Slightly more complicated with headers.标题稍微复杂一些。 There is two ways to use #include directive: with < > and " " braces有两种使用#include 指令的方法:使用 < > 和 " " 大括号

When " " braces used, compiler treats path as relative (if not absolute used) to location of cpp file .当使用“”大括号时,编译器将路径视为相对于 cpp 文件的位置(如果未使用绝对路径

When < > braces used, compiler looks for file in something like system include folders.当使用 < > 大括号时,编译器会在系统包含文件夹之类的东西中查找文件。 For example - stdlib headers folder and windows.h location folder.例如 - stdlib 头文件夹和 windows.h 位置文件夹。 Properties entry Additional Include Directories also goes there.属性条目附加包含目录也在那里。

I suggest you to change projects structure and extract shared features from both projects to compile it as static library.我建议您更改项目结构并从两个项目中提取共享功能以将其编译为静态库。 Place shared headers in some subfolder inside library project and refer then as将共享头文件放在库项目内的某个子文件夹中,然后将其引用为

#include "mylibHeaderDir/someheader.h"

In dependent projects, after setting Additional Include Directories you can refer theese includes as在依赖项目中,设置附加包含目录后,您可以将这些包含引用为

#include <myLibHeaderDir/someheader.h>

This approach will help you in future, as you can reuse that shared module in every project you want.这种方法将来会对您有所帮助,因为您可以在您想要的每个项目中重用该共享模块。

About how to create and link static library you can read this article http://msdn.microsoft.com/en-us/library/vstudio/ms235627(v=vs.110).aspx Version of visual studio may be differ, but basics are the same.关于如何创建和链接静态库,您可以阅读这篇文章http://msdn.microsoft.com/en-us/library/vstudio/ms235627(v=vs.110).aspx Visual Studio 的版本可能有所不同,但基本是一样的。

You can't just pick files like that.你不能只选择这样的文件。 There are two reasonable ways to solve it.有两种合理的方法可以解决。 1, shared the file by means of a Code Versioning System (eg svn/git). 1、通过代码版本控制系统(例如svn/git)共享文件。 2, compile the the .cpp into a library and link to that library. 2、将.cpp编译成一个库并链接到该库。

If the cpp can be used by multiple projects, it must mean that the code is something common.如果 cpp 可以被多个项目使用,那一定意味着代码是通用的。 That means you should compile that code by itself into a library and then share that library.这意味着您应该将该代码本身编译成一个库,然后共享该库。 Compiling the same cpp into multiple libraries is likely to result in conflicts later if two such libraries are ever needed to work together.如果需要两个这样的库一起工作,那么将相同的 cpp 编译到多个库中很可能会在以后导致冲突。

Try to drag them into your solution?尝试将它们拖到您的解决方案中? You can create a new folder in your solution, and drag them all into this folder!您可以在解决方案中创建一个新文件夹,然后将它们全部拖到该文件夹​​中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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