C 中的一对一链接
有没有办法在 C 中实现目标文件的链接,使得某些对象只能彼此链接在一起?
例如。尝试链接对象 1 和对象 2 将起作用并生成工作可执行文件,但尝试链接对象 1 和对象 3 将返回错误,或者生成的可执行文件将无法正确工作。
背景资料: 基本上,情况是程序员 A 和程序员 B 将他们的对象发送给链接器公司。 A 和 B 希望确保链接器公司只能将其对象链接在一起,而不能将其对象与其他程序员(即程序员 C 等)对象链接(因为这是非法的)。
Jörgen Sigvardsson 的答案很有用,但我想知道是否有更简单的方法。例如。如果尝试使用 gcc 将非法对象链接在一起,链接器公司可以确定需要哪些符号,然后将它们添加到非法对象中,从而轻松突破限制。
Is there any way to implement linking of object files in C, in such a way that certain objects can only be linked together with each other?
Eg. Trying to link Object 1 and Object 2 will work and produce a working executable, but trying to link Object 1 and Object 3 will either return an error, or the resulting executable will work incorrectly.
Background info:
Basically, the situation is Programmer A and Programmer B send their objects to a Linker Company. A and B want to ensure that the Linker Company can only link their objects together, and not link their objects with other Programmers (i.e. Programmer C etc) objects (as it would be illegal).
Jörgen Sigvardsson's answer is useful, but I am wondering if there is a more fool-proof method. Eg. If trying to link illegal objects together using gcc, the Linker Company could determine what symbols are required and just add them to illegal objects, easily defeating the restriction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,有办法。在对象 1 中:
在对象 2 中:
这将要求您将这些对象链接在一起,并阻止您将其与对象 3 链接在一起。这并不是万无一失的,但它可能会满足您的目的。
Yes, there is a way. In object 1:
And in object 2:
This will require you to link these objects together, and prevents you from linking it together with object 3. It isn't fool proof, but it may serve your purposes.
不,没有任何办法可以做到你所描述的。
No there is not any way to do what you described.
如果您想确保程序要么链接
foo1.o
和foo2.o
,要么都不链接它们,只需将它们组合成一个对象:并且只给出最终用户
foo-combined.o
如果您想实现其他目标,请更清楚地重申您的问题。
If you want to make sure that the program either links both
foo1.o
andfoo2.o
, or none of them, simply combine them into a single object:and only give the end-user
foo-combined.o
If you want to achieve something else, please restate your question more clearly.