简体   繁体   中英

c++ headers between projects

I run in trouble when writing application in c++. I have got two projects. Project A is ordinary c++ project, project B is c++ project with clr support. Project A contains classes that uses headers from LLVM framework. 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?

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. I cannot moved framework includes from header to cpp file. How can I solve it?

You can as long as you don't use templates in project A that depends on LLVM headers. You can always forward declarate the types you are using in project A headers and include the actual headers in the cpp files.

Say there is a type you want to use called llvm_type that is a class defined by the libraries of the LLVM compiler. In projectA.h you can do like this:

class llvm_type;

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

Then in your projectA.cpp you include the LLVM header:

#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. In this case you declare in your header something like class VectorInt; to define prototypes and in your cpp you write:

#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.

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