gpt4 book ai didi

c++ - c 和 c++ 与库的链接

转载 作者:行者123 更新时间:2023-11-30 17:59:28 25 4
gpt4 key购买 nike

假设我有一个使用类和模板的 C++ 库 lib.h。还假设我有一个自定义 C++ header 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);

现在假设我在一个名为 test.c 的 C 文件中。如下调用函数 foo() 是否合法?

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

此外,编译的目标代码/链接器阶段发生了什么?

谢谢!

最佳答案

Is it legal to call function foo() as follows?

 int first = foo(5, 6);

是的,这是合法的。尽管您应该阅读以下内容以确保此合法电话能够链接。

what is going on at the object code / linker phase of compilation?

类的使用不会造成干扰。 C++ 类将被编译为链接器能够理解的目标代码。

编辑克里斯·多德的评论:

您的模板也将通过 foo 调用来创建。

关于c++ - c 和 c++ 与库的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com