Ideal way to include headers from a DLL?

2019-08-02 08:38发布

I've been testing things with multiple project setups where a console application accesses functions from several DLLs.

I thought about how to include the DLLs' headers in the console application. My current implementation is as follows but it's being a headache to manage and even sometimes runs into errors:

  • Each DLL project has a folder called "Include"
  • The Console application project references the Include folder of each DLL project (as suggested by msdn guide to working with DLLs)
  • Each DLL project contains a single header that includes all the headers in that one project
  • The console application then #includes these "master headers"
  • Every project uses the pre-compiled header "stdafx" and each file #includes it.

It worked out fine until I started overloading operators. I believe the grief is caused by the pre-compiled header somehow, here's and example of my current usage of stdafx:

#define DLL // Found in every DLL, not in the console project

#ifdef DLL
#define DLLEI   __declspec(dllexport)
#else
#define DLLEI   __declspec(dllimport)
#endif

#include <iostream>
#include <vector>

#include "Include\Engine.h"

using namespace std;

With this I sometimes get some irrelevant random compiler errors that I can hackfix by excluding the header from the "master header" and including the troublemaker separately in the console app.

Suggestions what could be done better?

标签: c++ dll
2条回答
Fickle 薄情
2楼-- · 2019-08-02 09:05
不美不萌又怎样
3楼-- · 2019-08-02 09:16

__declspec(dllexport) and __declspec(dllimport) definitions should be placed in every public Dll include file, or at least in the main Dll public include file, which includes all other files. These definitions should not be in stdafx.h.

#define DLL // Found in every DLL, not in the console project

This is incorrect, every Dll must have unique preprocessor definition. In your case, of one Dll depends on another, it always compiles another Dll functions as __declspec(dllexport)

Ensure that every header has #pragma once in the beginning.

Consider using common Include directory for all projects.

As already mentioned in the comments, using namespace can be used only in source files.

查看更多
登录 后发表回答