简体   繁体   English

项目之间的C ++标头

[英]c++ headers between projects

I run in trouble when writing application in c++. 用C ++编写应用程序时遇到麻烦。 I have got two projects. 我有两个项目。 Project A is ordinary c++ project, project B is c++ project with clr support. 项目A是普通的c ++项目,项目B是具有clr支持的c ++项目。 Project A contains classes that uses headers from LLVM framework. 项目A包含使用LLVM框架中的标头的类。 Project B contains managed wrappers for some classes of project A. Can I use classes from project A in project B without including their headers? 项目B包含项目A某些类的托管包装。我可以在项目B中使用项目A中的类而不包含其标题吗?

The reason is this. 原因是这样的。 When I include class headers from project A in project BI need to specify also framework libraries that this header from project A uses, but LLVM framework cannot coop with clr support. 当我在项目BI中包含项目A的类头时,还需要指定项目A的此头所使用的框架库,但是LLVM框架无法支持clr支持。 I cannot moved framework includes from header to cpp file. 我无法将框架包含从标头移到cpp文件。 How can I solve it? 我该如何解决?

You can as long as you don't use templates in project A that depends on LLVM headers. 您可以在不依赖于LLVM标头的项目A中使用模板。 You can always forward declarate the types you are using in project A headers and include the actual headers in the cpp files. 您始终可以向前声明在项目A标头中使用的类型,并在cpp文件中包含实际标头。

Say there is a type you want to use called llvm_type that is a class defined by the libraries of the LLVM compiler. 假设您要使用一种名为llvm_type的类型,该类型是LLVM编译器的库定义的类。 In projectA.h you can do like this: projectA.h您可以这样:

class llvm_type;

class MyAPrjClass {
public:
    void myMethod (llvm_type x, int y);
}

Then in your projectA.cpp you include the LLVM header: 然后在projectA.cpp包含LLVM标头:

#include <llvm_type.h>

void MyAPrjClass:: myMethod (llvm_type x, int y)
{
    // Define your method using the llvm_type here.
}

The same can be done for structs, containers and other types as long as you forward declarate them. 只要您向前声明结构,容器和其他类型,就可以执行相同的操作。 Suppose for example you want to use a std::vector<int> that is part of the LLVM compiler. 例如,假设您要使用LLVM编译器中的std::vector<int> In this case you declare in your header something like class VectorInt; 在这种情况下,您可以在标头中声明类似class VectorInt; to define prototypes and in your cpp you write: 定义原型,并在您的cpp中编写:

#include <vector>
typedef std::vector<int> VectorInt;

and use it in your method definitions. 并在您的方法定义中使用它。

What you can't do is define template classes or methods that depend on LLVM headers because you need to define the methods in the same headers and that will involve knowing the implementation of the imported types. 您不能做的是定义依赖于LLVM标头的模板类或方法,因为您需要在相同的标头中定义方法,这将涉及了解导入类型的实现。

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

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