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 :
/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.
I had the same problem in the past. You have to declare the implementations of the inline functions as static inline
. static
means the function can only be linked in the same compilation unit (like it's private to this .c file). 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.