简体   繁体   English

c和c ++与库的链接

[英]c and c++ linkage with libraries

Suppose I have a C++ library lib.h that uses classes and templates. 假设我有一个使用类和模板的C ++库lib.h。 Suppose also that I have a custom C++ header myLink.h with the following: 还假设我有一个带有以下内容的自定义C ++头文件myLink.h:

#include "lib.h"

  //call methods from lib.h that use templates and classes
  //  and return an integer based off of the information gained from calling functions lib.h  
  extern "C" int foo(int param1, const int param2);

Now suppose I am in a C file called test.c. 现在假设我在一个名为test.c的C文件中。 Is it legal to call function foo() as follows? 如下调用函数foo()是否合法?

//in test.c
int first = foo(5, 6);

Also, what is going on at the object code / linker phase of compilation? 另外,在编译的目标代码/链接器阶段发生了什么?

Thanks! 谢谢!

Is it legal to call function foo() as follows? 如下调用函数foo()是否合法?

 int first = foo(5, 6);

Yes, it's legal. 是的,这是合法的。 Although you should read below to make sure this legal call will link. 尽管您应该阅读以下内容以确保此法律电话将链接。

what is going on at the object code / linker phase of compilation? 编译的目标代码/链接器阶段发生了什么?

The use of classes won't interfere. 使用类不会干扰。 C++ classes will be compiled to object code that the linker will understand. C ++类将被编译为链接器将理解的目标代码。

Edit from Chris Dodd's comment: 编辑克里斯·多德的评论:

Your templates will also be created by virtue of foo calling them. 您的模板也将通过foo调用它们来创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM