C ++ Include from Internet

I want to include a headline from the Internet. For example: the file add.h and add.cpp were sent to github.com

file add.h is int add (int a, int b);

file add.cpp is int add (int a, int b) {return a + b;}

in my main.cpp I need code like this

#include "github.com/xxx/add.h" int main(){ int a = add(1,1); } 

When compilation starts, the compiler can automatically download add.cpp from github.com

Could this happen?

+5
source share
2 answers

No, this will not happen (assuming you are not using some sort of selection mechanism). Only prefix files are supplied only by local files with the #include directive.

C ++ doesn't work like Go or Javascript

Files hosted on github must be extracted using git and then used.

I recommend reading the C ++ and Git book before continuing (or your compilation / assembly guide to add extra fetch steps)

+4
source

C ++ does not support this function natively. You will have to build it yourself.

You can add an extra step to your build system, which

  • parses the source code for inclusions, like the one in the above example
  • loads the included files (and also requires *.cpp somehow)
  • Generates an updated version of the source code that references the downloaded files.
  • then continues assembly as usual

This is a lot of work .

Better look at something like CMake's ExternalProjects . This feature can download, create, and install dependencies so your project can use it. Of course there are alternative technologies (@ commentators, please add your favorites).

UPDATE: (thanks @Angew)

Technically, C ++ does not indicate how the argument of the #include directive is processed. These are all well-known compilers that "do not support this function initially", and not C ++ itself. The compiler that supported this can be completely standard.

+4
source

All Articles