英文:
Force Linker Error on Function Call To Library
问题
我想要在我调用第三方库中的某个函数时强制生成错误或警告,因为我希望避免需要进行审核来确保我们不调用该函数。
谷歌给我提供了一些非常古老的StackOverflow答案(例如https://stackoverflow.com/questions/10727418/can-i-force-a-compiler-error-if-certain-functions-are-called),这些答案基于在代码中添加#defines或#deprecated提示,这些提示基于预处理,但为了避免与命名产生任何奇怪的问题,我想知道是否有任何现代的可能性来实际告诉链接器,我想要链接到库libfoo,但如果我的代码中有人链接到libfoo中的特定调用,请生成错误。
注意:这不是用作对抗恶意行为的安全保护措施,我只想自动化检测我们是否意外调用了该函数的过程。
英文:
I would like to force an error or warning if I call a certain function from a third-party lib because I want to avoid needing reviews to make sure we do not call that function.
Google gives me very old StackOverflow answers to that problem at the top (e.g. https://stackoverflow.com/questions/10727418/can-i-force-a-compiler-error-if-certain-functions-are-called) which are based on adding #defines or #deprecated hints in the code which are based on preprocessing, but to avoid any weird issues with the naming there I was wondering whether there is any modern possibility to actually tell the linker that I want to link against library libfoo, but please error out if someone in my code links against a specific call in libfoo.
NOTE: This is not meant as a security protection against a malevolent actor, I only want to automate the process of detecting that we call that function by accident.
答案1
得分: 1
如果你想在编译时而不是运行时执行此操作,可以使用static_assert(condition, message)
。这是迄今为止最干净和最现代化的方法。这里有一些关于它的信息:链接。这是纯粹的C++,不需要任何库。
示例:
void foo(int a)
{
static_assert(a > 5, "太大了!");
}
如果你想在运行时获取它,可以抛出异常throw runtime_error("示例");
。
希望能帮到你。
英文:
Sure, there are many ways of doing that.
If you want to do this during compile time, before runtime, you can use static_assert(condition, message)
. This is the the cleanest and modernist way thus far. Here is some info on that: Link. It is pure C++, no library necessary.
Example:
void foo(int a)
{
static_assert(a > 5, "Too big!");
}
If you want to get it during runtime, you can throw exceptions throw runtime_error("Example");
.
Hope that helps.
答案2
得分: 0
你可以使用属性 deprecated
或 warning("message")
来在编译时诊断这个问题:
$ cat tmp.c
__attribute__((warning("do not use me"))) void foo() {}
int main() { foo(); return 0; }
$ gcc tmp.c
tmp2.c: In function 'main':
tmp2.c:2:14: warning: call to 'foo' declared with attribute warning: do not use me [-Wattribute-warning]
这是一种首选的方法,因为它允许早期诊断错误并提供精确的源代码位置。
在Linux上,你还可以使用 .gnu.warning
段在链接时发出警告:
$ cat foo.c
void foo() {}
static const char foo_warning[] __attribute__((used,section(".gnu.warning.foo"))) = "do not use me";
$ cat main.c
void foo();
int main() { foo(); return 0; }
$ gcc foo.c main.c
/usr/bin/ld: /tmp/ccPfdczm.o: in function 'main':
main.c:(.text+0xe): warning: do not use me
请注意,这些代码示例是用英文注释的,但已根据你的要求进行了翻译。
英文:
You could use attributes deprecated
or warning("message")
to diagnose this at compile time:
$ cat tmp.c
__attribute__((warning("do not use me"))) void foo() {}
int main() { foo(); return 0; }
$ gcc tmp.c
tmp2.c: In function ‘main’:
tmp2.c:2:14: warning: call to ‘foo’ declared with attribute warning: do not use me [-Wattribute-warning]
This is a preferred approach as it will allow to diagnose errors early and with precise source code location.
On Linux you can also use a .gnu.warning
section to emit warning at link time:
$ cat foo.c
void foo() {}
static const char foo_warning[] __attribute__((used,section(".gnu.warning.foo"))) = "do not use me";
$ cat main.c
void foo();
int main() { foo(); return 0; }
$ gcc foo.c main.c
/usr/bin/ld: /tmp/ccPfdczm.o: in function `main':
main.c:(.text+0xe): warning: do not use me
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论