如何确保没有代码使用API​​? [英] How can I ensure no code uses an API?

查看:100
本文介绍了如何确保没有代码使用API​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁止使用iostreams在一个代码库我有(由于各种原因)。是否有一种方法可以检查符号文件或强制编译器在使用该API时发出错误?

解决方案

方法提供了一个假的 iostream 实现,它不会引发编译时错误。





首先,创建你的虚拟iostream文件:

  #error'禁止使用iostream'

虚拟应用程序代码来演示:

  #include< iostream> 

int main(int argc,char ** argv){
std :: cout< foo!;
return 0;
}

编译如下(假设虚拟 iostream main.cpp 位于工作目录中):

  g ++ -I。 main.cpp 

编译失败,出现以下错误:


$ b b

 在从main.cpp:2:0中包含的文件中:
./iostream:1:2:error:#error'禁止使用iostream'
main.cpp:在函数'int main(int,char **)':
main.cpp:4:2:error:'cout'不是'std'的成员

添加奖励:通常在该文件中声明的符号(例如 cout )未定义,因此在编译器输出中也被标记。因此,您还可以获得指向正确使用禁止的API的位置。



UPDATE: Visual C ++ 2012的说明。 p>

正如@Raymond Chen在下面的评论中指出的,针对Visual C ++定制的解决方案可能对OP更有用。因此,以下概述了我在Visual C ++ 2012中实现与上述相同的过程。



首先,使用上述C ++创建一个新的控制台项目码。同时创建我上面描述的虚拟iostream头,并将它放在一个容易找到的目录(我放在主项目源目录中)。



现在,解决方案资源管理器,右键单击项目节点并从下拉列表中选择属性。在出现的对话框中,从左侧的树中选择VC ++目录。将包含虚拟iostream文件的目录添加到右侧显示的包含目录列表中,与其他目录分开,使用分号:





这里,我的项目称为TestApp1,我只是把它的主目录添加到已经存在的 $(IncludePath)。注意,重要的是前缀而不是append - 列表中的目录的顺序决定搜索顺序,因此如果 $(IncludePath) em>您的自定义目录,系统标题将优先于您的虚拟标题 - 而不是您想要的。



单击确定,然后重建项目。 >

对我来说,这样做导致VC ++控制台中出现以下错误(为简洁起见略作编辑):

 错误C1189:#error:禁止使用iostream
IntelliSense:#error指令:禁止使用iostream
IntelliSense:命名空间std没有成员cout

请注意,IntelliSense还会拾取使用 cout - 它在编辑器中以错误标记突出显示。


I want to ban use of iostreams in a code base I have (for various reasons). Is there a way I can inspect symbol files or force the compiler to emit an error when that API is used?

解决方案

A simple approach is provide a dummy iostream implementation that does nothing but throw a compile-time error.

The following example assumes a GCC toolchain - I imagine the process is similar with other compilers.

First, create your dummy iostream file:

#error 'Use of iostream is prohibited'

Some dummy application code to demonstrate:

#include <iostream>

int main (int argc, char** argv) {
    std::cout << "foo!";
    return 0;
}

Compile as follows (assuming the dummy iostream and main.cpp are in the working directory):

g++ -I. main.cpp

Compilation fails with the following errors:

In file included from main.cpp:2:0:
./iostream:1:2: error: #error 'Use of iostream is prohibited'
main.cpp: In function 'int main(int, char**)':
main.cpp:4:2: error: 'cout' is not a member of 'std'

Added bonus: symbols usually declared in that file (e.g. cout here) are undefined, and so get flagged in the compiler output as well. As such, you also get pointers to exactly where you're using your prohibited API.

UPDATE: Instructions for Visual C++ 2012.

As @RaymondChen points out in the comments below, a solution tailored to Visual C++ is likely more useful to the OP. As such, the following outlines the process I went through to achieve the same as the above under Visual C++ 2012.

First, create a new console project, using the above C++ code. Also create the dummy iostream header I described above, and place it in a directory somewhere easy to find (I put mine in the main project source directory).

Now, in the Solution Explorer, right click on the project node and select "Properties" from the drop-down list. In the dialog that appears, select "VC++ Directories" from the tree on the left. Prepend the directory containing the dummy iostream file into the list of include directories that appears on the right, separated from the other directories with a semicolon:

Here, my project was called TestApp1, and I just prepended its main directory to the $(IncludePath) that was already there. Note that it is important to prepend rather than append - the order of the directories in the list determines the search order, so if $(IncludePath) appears before your custom directory, the system header will be used in preference to your dummy header - not what you want.

Click OK, and rebuild the project.

For me, doing so resulted in the following errors in the VC++ console (edited slightly for brevity):

error C1189: #error :  'Use of iostream is prohibited'
IntelliSense: #error directive: 'Use of iostream is prohibited'
IntelliSense: namespace "std" has no member "cout"

Note that IntelliSense also picks up the (now) illegal use of cout - it is highlighted with an error mark in the editor.

这篇关于如何确保没有代码使用API​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆