How to export multiple header files as a single header file in C++?

血红的双手。 提交于 2019-12-13 18:43:34

问题


I'm developing a DLL using Visual Studio. Currently, I have one header file: MyProject.h:

#pragma once

#ifdef MYLIBRARY_EXPORTS
#define MYLIBRARY_API __declspec(dllexport)
#else
#define MYLIBRARY_API __declspec(dllimport)
#endif

#include <map>
#include <string>

extern "C" class MYLIBRARY_API Class1
{
    // some stuff here...
};

extern "C" class MYLIBRARY_API Class2
{
    // some stuff here
};

I am going to be adding some more classes to the project and I would like to add separate header files for each class (please advise if I should not do that). However, eventually, I want to package all that into a single .dll and .lib in such a way that the client only needs to include a single header file. That is, the client project has #include "MyProject.h" which is essentially a collection of header files with their implmentation files. Is this possible and how can I achieve this? Thanks.

EDIT:

To be specific, what I want to do is to put Class1 in Class1.h and Class2 in Class2.h and include both of them in one master header file called MyLibrary.h so that a client only have to do #include "MyLibrary.h".


回答1:


If you have several header files like A.h, B.h etc and want to give the client just Project.h that includes all, then simply include all the headers in Project.h - like so:

#ifndef MY_PROJECT_H
#define MY_PROJECT_H
#include "A.h"
#include "B.h"
#endif



回答2:


Sorry ,I misunderstood what you were asking.

Yes, you can have multiple header-files that are included in your library header-file (be sure to include them with #include "...").

if you want to eliminate the need of specifying library in the linker-section (this is what I thought you were asking) ,with MSVC++ the thing you're looking for is

#pragma comment( comment-type [,"commentstring"] )

https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp



来源:https://stackoverflow.com/questions/47985779/how-to-export-multiple-header-files-as-a-single-header-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!