-1

I have one lib which contains a file (called Util.c down here), and i'm trying to use some funtions defined in this file to compile a little prog. I'm already using others functions from the lib, but the ones of this particular file always give me back an "Undefined Reference". What's really weird is: the others functions from the lib does use functions from Util.c, and that's working well. It's just that...i can't use them directly.

I'll try to give you a clear view of the things here:

Util.c

void foo (void *var){
    print ("in Foo!");
}

void *bar (int var){
    print ("in bar!");
}

Util.h:

#ifndef UTIL_DEFINED
#define UTIL_DEFINED

void foo(void *var)
void *bar(int var)

#endif

libobj.c:

#include "Util.c"
// #include "a lot of other files.c"

MyFile.c:

#include "Util.h"
// #include "others headers from the lib which functions are working well"

int main(){
//here i use the lib's function without a prob, except for the ones coming from Util.c, namely foo and bar. For example: 

int *var;
//Say that LibFunc is another function from the lib, but not from Util.c. 
//Libfunc make a call to foo, and it's working well. 
Libfunc(var);

//However, here i try to use foo or bar, and i get the undefined reference!
foo(var);

bar(var);

}

The Makefile is a big thing, but if i search only for the parts related to the lib, it looks like it:

Lib Makefile

default: $(LIB)
$(LIB): $(LIBOBJ)
    -$(RM) "$@"
    $(DLLD) -o "$@" $(DLLDFLAGS) "$(LIBOBJ)"
$(LIBOBJ): libobj.c $(LIBSRC) $(LIBINC)
    $(CC) -c $(LOTOFFLAGS) libobj.c -o "$@"

LIBSRC: libobj.c\
    aBunchOfOtherFiles.c
LIBINC: Util.h\ 
    ABunchOfHeadersFiles.h 

LIBOBJ=$(LIBNAME)dll.o
LIB=$(LIBNAME).dll


CC= gcc
DLLD = gcc -shared -fprofile-arcs -ftest-coverage -fprofile-generate
DLLDFLAGS = -fPIC
LOTOFFLAGS = -Wall -pedantic -Wno-long-long -Wno-unused-function -g -g3 -ggdb -std=c99 --coverage -fprofile-arcs -ftests-coverage -fprofile-generate -Wl,--allow-multiple-definition -fPIC $(IFLAGS)
#IFLAGS include all headers files, as well as some info concerning Lib version, OS, PROC, etc... not so important here. 
IFLAGS = -I$(FOLDERS) -DWINDOWS
FOLDERS= MyFolders
LIBNAME = MyLibName

RM= del

And finally, my makefile looks like it:

My Makefile

default: $(PROG_EXE)

$(PROG_EXE) : $(LIB) $(PROG_OBJ) 
    $(CC) $(SOMEFLAGS) $(PROG_OBJ) "$(LIB)" -o "$@"
$(PROG_OBJ) : MyFile.c $(LIBINC)
    $(CC) -c $(SOMEFLAGS) -$(IFLAGS) MyFile.c -o "$@"

LIB = $(LIBNAME).dll
LIBINC = Util.h\
    ABunchOfHeadersFiles.h 

PROG_EXE= MyProg
PROG_OBJ = MyProg.o

CC= gcc
SOMFLAGS = -Wall -std=c99 -pedantic -g -g3 - ggb -fprofile-arcs -ftests-coverage 
#IFLAGS basically include all folders containing source code and headers. Also gives some D, such as OS, PROC or the lib version. Not really important for my prob. 
IFLAGS = -I$(FOLDERS) -DWINDOWS 
FOLDERS = MyFolders
LIBNAME = MyLibName

Note that i didn't made any of that. I'm just supposed to make it works...

I've tried to get all the flags (think i got them all). Some of them are only for the use of gcov, others just to include the good folders and files. I've already checked: Util.c and Util.h are correctly included in thoses. BTW, when i compil MyProg, i don't have the "file not found" error on Util.h, so it is found.

Also, i've seen that foo is supposed to get a void* and is actually given a int*. But i don't think it's the problem here, since when i compile MyProg using directly Util.c, without trying to get it from the lib, it works well.

So i really think that, somehow, my prog can't find the functions in the lib. But it does found the header well, and it does found others functions from the lib well, and those functions use the ones froms Util.c, so i just don't get it :'( Plus, i've tried to compile the lib WITHOUT Util.c, just to be sure that the lib really uses it. Then i got the same "undefined reference", so it seems that the lib correctly includes Util.c

PS: i'm on windows 32bits, if that is of any use...

Ablia
  • 191
  • 2
  • 10
  • 1
    It would help to see in your question the exact linker command lines that are executed for your program and the library. – Bodo Jul 12 '19 at 10:55
  • You need to post the **actual code** that you’re using. The code you’ve posted doesn’t compile because it contains numerous syntax errors. – Konrad Rudolph Jul 12 '19 at 11:33
  • I can't post the **actual code**. First, because it is far too huge, tooks me hours to get the files linked to my problem and get rid of everything else, second because the code is on a PC without internet and i'm not allowed to move it, third for secrecy. However, i tried to make it minimal and executable. – Ablia Jul 12 '19 at 12:25
  • I found out that the other Lib function i'm using do make some call to the functions from Util.c, and that works. How can i use functions of the lib, which uses functions of Util.c (also in lib), but i'm not able to use directly functions of Util.c? It makes totally no sense to me. – Ablia Jul 12 '19 at 14:29

2 Answers2

0

I think the mistake comes from libobj.c file:

#include Util.c

The quotation marks are missing:

#include "Util.c"

In General we #include the .h and not the .c.

Mayih
  • 1
  • sorry, but no, that was just a typo from me. I've also never seen such .c includes, no idea how it works. This file is really just a bunch of #include with .c, nothing more :/ – Ablia Jul 12 '19 at 12:15
0

I've found a way to make it works, even if i still can't understand the problem.

It seems that, in Windows using Mingw, you have to add __declspec( dllexport ) in front of each function you wanna use from outside of the dll.

So, if i change my Util.c this way:

`__declspec( dllexport ) void foo (void *var){
    print ("in Foo!");
}

`__declspec( dllexport ) void *bar (int var){
    print ("in bar!");
}

I think this lib is somehow set so that it has "publics" and "privates" functions; until now i was just using the public ones, and the ones inside Util.c are private. It's the only explanation i see, even if i had no idea how to makes function privates or public.

Ablia
  • 191
  • 2
  • 10