Protecting specific included locations

I am creating a small language that will compile in C or C ++, I have not decided yet, however I ran into a dilemma regarding the keyword #include.

My language will come with a standard library, which will be included in the language and will be available in the same way as in C or C ++, including the standard #include <string>.

My compiler can automatically tell the difference between the user and the standard library , but my problem is how the GCC compiler uses the flag -I.

Take Java as an example. One of the default packages (folder) is called java.util. If I try to create my own folder called java.utilinside my project, I get an error:

The java.util package conflicts with a package available from another module: java.base

The default value is enabled.

I would like this to do the same in C ++, but I'm worried that the user may (hypothetically) include a relative path and cause a conflict.

Take, for example, I use the flag as follows: -I ../some/folder.

However, then the user can simply enter #include "../some/folder"to access the same content. Is there any way to limit this, and, like the name of the question, offers to "protect" the folder from the so-called?

, test.h, test.h . ? . ?

: ( , )

boolean userDefine = false;
string defineName = "foo";

// Do something to determine if <> define or "" define.

if (userDefine) {
    // Returns #include "foo"
    return "#include \"" + defineName + "\"";
} else {
    // Returns #include "stdlib/foo"
    return "#include \"stdlib/" + defineName + "\"";
}

, .

+4
1

#include ++ .

, a #include ++, , , ++ github, , #include .

, , , - , #include #ifndef/#define . , , .

, - #undef , . ++ , ; ? , #pragma once, ...

+2

Source: https://habr.com/ru/post/1693072/


All Articles