相关疑难解决方法(0)

内联函数的多重定义

我已经阅读了一些与此主题相关的帖子,但无法彻底理清我的疑问.这可能是一个非常天真的问题.

我有一个头文件inline.h和两个翻译单元main.cpptran.cpp.

代码详情如下

inline.h

#ifndef __HEADER__
#include <stdio.h>
extern inline int func1(void)
{ return 5; }

static inline int func2(void)
{ return 6; }

inline int func3(void)
{ return 7; }
#endif
Run Code Online (Sandbox Code Playgroud)

main.c中

#define <stdio.h>
#include <inline.h>
int main(int argc, char *argv[])
{
    printf("%d\n",func1());
    printf("%d\n",func2());
    printf("%d\n",func3());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

tran.cpp

//(note that the functions are not inline here)
#include <stdio.h>
int func1(void)
{ return 500; }

int func2(void)
{ return 600; }

int func3(void) …
Run Code Online (Sandbox Code Playgroud)

c inline

25
推荐指数
2
解决办法
1万
查看次数

简单的C内联链接器错误

简单的问题:

给出以下程序:

#include <stdio.h>

inline void addEmUp(int a, int b, int * result)
{
    if (result) {
        *result = a+b;
    }
}

int main(int argc, const char * argv[])
{
    int i;
    addEmUp(1, 2, &i);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到链接器错误...

Undefined symbols for architecture x86_64:
  _addEmUp", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

看起来似乎没有费心去编译它.

它不应该是static,我不会想,基于我读过的内容:
链接器错误内联函数(因为这是在一个不同的对象,并处理2个定义而不是零)

这是一个相关的链接,但它是c ++,我认为在std C中将代码放入头文件中是不错的做法: …

c inline linker-errors

7
推荐指数
1
解决办法
5249
查看次数

标签 统计

c ×2

inline ×2

linker-errors ×1