0

My situation: I'm writing DLL, it has many classes, but I export only one:

#pragma once

#ifdef APIDLL_EXPORTS
#define APIDLL_API __declspec(dllexport)
#else
#define APIDLL_API __declspec(dllimport)
#endif

class Process; // <-- ApiClient uses pointer to that class, so I forward declare it

class APIDLL_API ApiClient
{
    web::http::client::http_client client; // (*) line
public:
    void check(Process * p);
}

And In ApiClient.cpp I have #include "pch.h", which is:

#ifndef PCH_H
#define PCH_H

// add headers that you want to pre-compile here
#include "framework.h"

#include <sstream>
#include <iomanip>

#include <cpprest/http_client.h>
#include <cpprest/json.h>
#include <cpprest/asyncrt_utils.h>

#endif //PCH_H

As it is compiling as DLL, it won't compile when I link it to console app. I put in includes directory ApiClient.h (because I export and need only this) (path to .lib is correct). I get error in (*) line E0276 name followed by '::' must be a class or namespace name. It looks like compiler can't see definition of http_client. So if I add #include <cpprest/http_client.h> in ApiClient.h, all is good.

But I'd like to have every needed dependency in 'pch.h'. Should I copy it to includes directory also? But what if I add some relative paths to pch.h?

colorgreen
  • 265
  • 1
  • 2
  • 12
  • 1
    The pch.h file shouldn't be a part of the equation. Make the header file that contains your interface include what *it* needs. You should be able to use the DLL in projects where pch.h doesn't even exist. – Ted Lyngmo Sep 05 '19 at 21:37
  • @TedLyngmo okay. In my case vcpkg integration made cpprestsdk library accessible everywhere. If I will send DLL to someone, should I also provide headers for that cpprestsdk? Or is there a way to compile it staticly? – colorgreen Sep 05 '19 at 21:40
  • You need to supply headers containing the classes/functions/enums etc that you want the users to use. Stuff that is only used internally in the DLL doesn't need to be included. The PCH.H files should definitely not be included in the package. – Ted Lyngmo Sep 05 '19 at 21:44
  • If someone needs to include a header to get declarations and definitions, they need the header. Do what you can to hide what you don't want the client to see. Perhaps [PIMPL](https://en.cppreference.com/w/cpp/language/pimpl) may be of use to you. – user4581301 Sep 05 '19 at 21:45
  • Header files need to be self contained, that is have corresponding `#inlcude` for each symbol used. You also seem to misuse precompiled header. It is not a file to dump every needed dependency to, it is a file that includes third-party-rarely-changing headers included in other files in order to speedup compilation. – user7860670 Sep 05 '19 at 21:52
  • I turned off user-wide integration, so I haven't paths to cpprestsdk headers files, I'm trying to compile my DLL and I get error `cannot open source file "cpprest/http_client.h"` near `#include ` :(. Should I add that includes after `__declspec(dllimport)` ? – colorgreen Sep 05 '19 at 22:02
  • Are you aware of how include files work in C++? Trying to build stuff with trial and error is not very productive... – user7860670 Sep 05 '19 at 22:09
  • Yea, I know. Ok, I have added in Additional Includes path to cppressdk headers files, it works now. I just thought that my lib could self include that files, nevermind, now is ok, thanks for help – colorgreen Sep 05 '19 at 22:12

0 Answers0