0

This static C library (libabc.a) is linked to a C++ program.

In my pro file for qmake, I have used
LIBS += pathToLib/libabc.a I had no problems when I created the static library. When using qmake and gmake to compile and run the cpp application, the object files for other cpp files are created but I get the following error:

../abc/libabc.a(mdl.o): In function 'SetExt': abc/src/mdl.c:2186: undefined reference to 'func1'

In mdl.c, both declaration and definition is there.
static void func1(int *, char *, char *);

static void func1(int *m, char *p, char *s)
{
.....
}

The function call of this function is in the C file mdl.c. Have I made a mistake when creating the static library (using gcc and ar)? or, what am I missing out?

vinu960
  • 33
  • 6
  • @Incomputable not seeing an extern doesn't make it not both. It means that the linker's going to get confused later. I'm not the one you should be telling that to though – UKMonkey Dec 01 '17 at 11:46
  • @UKMonkey, thanks for the info. Is there any resource to read up about it? – Incomputable Dec 01 '17 at 11:47
  • @Incomputable https://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c – UKMonkey Dec 01 '17 at 11:48

1 Answers1

1

You declared your function as

static void func1(int *, char *, char *);

Per 6.2.2 Linkages of identifiers, paragraph 3 of the C Standard:

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

Thus, func1() isn't accessible from outside of the compilation unit - it can only be used in the source file it's in.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • Yes I aware of this.. but my doubt is that since this function is called in that particular c file that it is defined in, yet why is the error thrown then? In mdl.c func1() is declared., defined and called... This function is not called in any of the cpp files... – vinu960 Dec 01 '17 at 15:12
  • You mean to say I cant even link this object file to other programs eventhough that function is not called there in that cpp program? – vinu960 Dec 01 '17 at 15:13
  • @vinu960 How are you compiling your code? You tagged it C **and** C++, yet only C code is mentioned. Did you forget an `extern "C" ...` somewhere? – Andrew Henle Dec 01 '17 at 15:28