1

I'm planning to build a C++ project to a DLL for other C++ projects to use. When building the DLL, as you may assume I want all compiled header objects from the project to be linked into the DLL build. However, I don't want all those headers to be accessible to users of the DLL.

For example, let's say some DLL project has the following headers:

FORFRUITSONLY.h
orange.h
apple.h

orange.h and apple.h both #include FORFRUITSONLY.h or else they wouldn't function. However, I want users of this DLL to only be able to #include orange.h and/or apple.h with the desire for the compiler to fail if they try to #include FORFRUITSONLY.h. How can I achieve this?

6
  • 2
    look into the pimpl technique/idiom Commented Sep 22, 2024 at 22:09
  • @AhmedAEK Will do, thank you! Commented Sep 22, 2024 at 22:13
  • 2
    You've described your requirement in terms of how you think you can achieve it. See XY problem. What is your real goal? Why should compilation fail if someone tries to #include "FORFRUITSONLY.h"? Even if you accomplish this, they could simply instead #include "apple.h" and thereby access everything they could if they were to include the forbidden file directly (inclusion is transitive). There are ways to prevent direct inclusion, but I suspect they would not help you meet your real goal. (PIMPL might.) Commented Sep 22, 2024 at 22:54
  • It's funny you mention XY problem, this question was actually written in direct response to realizing my original goal WAS an XY problem (and furthermore symptomatic of a failing design). I could go into it but there's a character limit. I suppose what I "really" want to do is I want to make certain methods of a class only accessible if being called from other classes/structs in the DLL (see: failing design). I've been looking into PIMPL but I don't see how it'd be functionally any different to making the methods private which prevents other DLL .h and .cpp files from using the methods. Commented Sep 22, 2024 at 23:00
  • 3
    The point of using PIMPL is that you can avoid #includeing FORFRUITSONLY.h in orange.h and apple.h and only include it in orange.cpp and apple.cpp. That way you don't need to ship FORFRUITSONLY.h with your library and thus consumers can't #include it. Commented Sep 22, 2024 at 23:13

1 Answer 1

3

My usual approach to this problem is to split the DLL header into two parts, one is a project-internal header and the other is the public header meant to be included from other projects.

The tricky part here is that anything that is part of the public interface will need to be included.

For example, if you export a type with a std::string member, then you will need #include <string> as part of the public header.

One suggestion would also be to look into the PIMPL idiom if you have not already done so, and make the public interface as lean as possible. Unlike with template libraries, the client of a DLL needs no (or at least very limited) knowledge of how the library operates internally.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.