Question

In my case, I have two layers say core layer and application layer. Application layer depends of core layer.

I want that only core layer should use CRT functions.

In application layer, if any CRT function is used, it should not compile.

Is there any way to achieve this? For example, direct call to free/malloc should not be made in application layer.

One way which I thought to #define all CRT functions to some error so that Application layer cannot use direct CRT calls (Application layer is including the header files of core layer).

Was it helpful?

Solution

You don't need to #define all the funcs in CRT. It's enough to define one of the funcs declared in the header to cause compilation failure.

Also, look into the CRT headers, most of them rely on some construct of this kind:

    #ifndef "some unique id"
    #define "some unique id"
    /* header body */
    #endif

If you define this unique id, you'll effectively cause the header not to be included, thus compilation error will occur when trying to use function declared in this header.

OTHER TIPS

Assuming all your projects are native C or C++, I believe removing the references to the windows CRT binaries should suffice. If anyone tries to use them, they'll get link errors.

For a dll, open the project properties (from Visual Studio), then go to Configuration Properties->Linker->Input and set Ignore All Default Libraries to Yes (/NODEFAULTLIB). Right above that one, set Additional Dependencies to just the libraries from your solution that you need.

For a static library, go to Librarian->General and do the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top