Ideal way to include headers from a DLL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 14:40:09

问题


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?


回答1:


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




回答2:


This article answered quite a few of my questions.

http://www.codeproject.com/Articles/6351/Regular-DLL-Tutor-For-Beginners



来源:https://stackoverflow.com/questions/15362364/ideal-way-to-include-headers-from-a-dll

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