1

I have defined an inline function copy_string in file cpstr.c and created .so file (libtest.so) for cpstr.c file. While trying to link this libtest.so for test.c, I am getting an error as

ild: (undefined symbol) char*copy_string(char*,const char*) -- referenced in the text segment of test.o

When I removed inline from the function copy_string, it works fine.

Below are the commands we tried,

CC  -c  -xarch=v9  test.c

CC -G  -xarch=v9 -o libtest.so -Kpic cpstr.c

CC -xarch=v9   -g -o test  test.o  /myplace/libtest.so 

When we tried to get the contents of libtest.so , I couldn't find copy_string name in libtest.so file . But I can see it in the contents when I removed 'inline' from copy_string function .

Can anyone please suggest me with a solution to get rid of undefined symbol error without removing inline function.

test.c

#include <stdio.h>
extern char  *copy_string (char *, const char*);
int main()
{
    char str[50];
    copy_string(str,"hello");
    printf("%s\n", str);
    return 0;
}

cpstr.c

#include<string.h>
inline char    *copy_string (char *str1, const char *str2)
{
     return (str2 ? strcpy (str1, str2) : (char *) 0); 
}

CC  -c  -xarch=v9  test.c
CC -G  -xarch=v9 -o libtest.so -Kpic cpstr.c

CC -xarch=v9   -g -o test  test.o  /space/systpe/devendra/dhsqlroot/libtest.so 
ild: (undefined symbol) char*copy_string(char*,const char*) -- referenced in the text segment of test.o
Jojo
  • 1,875
  • 3
  • 29
  • 29

2 Answers2

0

It wants you to implement your inline function in the header file

spiritwolfform
  • 2,263
  • 15
  • 16
  • Thank you. I moved that inline function to a header file(cpstr.h) and #included this header file in a .c file(test2.c) and created .so file. But still I am getting same error while trying to link this libtest.so for test.c. Is there any solution without adding cpstr.h to test.c directly. – user2931858 Feb 25 '14 at 13:42
0

Functions with the function specifier inline shall be defined in each module where they are used. The compiler need to see their inline definitions that to generate correctly the object code. So usually their definition are placed in a header.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you. I moved that inline function to a header file(cpstr.h) and #included this header file in a .c file(test2.c) and created .so file. But still I am getting same error while trying to link this libtest.so for test.c. Is there any solution without adding cpstr.h to test.c directly – user2931858 Feb 25 '14 at 13:47
  • 1
    @user2931858 Instead of extern char *copy_string (char *, const char*); you should place its definition in test.c – Vlad from Moscow Feb 25 '14 at 13:51
  • Thanks, But I observed that the same program (similar logic) works in Linux platform as the .so file contains copy_string name in its contents. Is there any flag using which adds inline function name to .so file while creating it in Solaris(I observed if I remove inline I can see function name in .so file contents but it disappers if I add inline). Please let me know if any flags available to get function name in .so file with inline functions. – user2931858 Feb 26 '14 at 10:25