简体   繁体   English

对gcc pthreads C中函数的未定义引用

[英]undefined reference to function in gcc pthreads C

I have a code with pthreads named par.c , but when I try to compile it with the command: gcc par.c -lpthread -o par , it gives me this : 我有一个带有名为par.c pthread的代码,但是当我尝试使用以下命令对其进行编译时: gcc par.c -lpthread -o par ,它给了我这个:

/tmp/ccAzMTL8.o: In function `compare': par.c:(.text+0x414): undefined reference to `exchange'

the function is 功能是

inline void exchange(int i, int j) {
  int t;
  t = a[i];
  a[i] = a[j];
  a[j] = t;
}

inline void compare(int i, int j, int dir) {
  if (dir==(a[i]>a[j])) 
    exchange(i,j);
}

And in main I have them both: 总的来说,我两个都有:

void compare(int i, int j, int dir);
inline void exchange(int i, int j);

Does anyone have any ideas why is this happening? 有谁知道为什么会这样吗?

I'm assuming all the code you posted is in the same .c file. 我假设您发布的所有代码都在同一个.c文件中。

I had the same problem in the past. 我过去也遇到过同样的问题。 You have to declare the implementations of the inline functions as static inline . 您必须将内联函数的实现声明为static inline static means the function can only be linked in the same compilation unit (like it's private to this .c file). static意味着该函数只能在同一编译单元中链接(例如,此.c文件专用)。 This should be the case for inline function anyway, so I can't tell you why it makes a difference, but it worked for me. 无论如何,内联函数都应该是这种情况,因此我无法告诉您为什么会有所不同,但是它对我有用。

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

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