0

I use gcc and unity build. When I have a piece of code in a ".a" file compiled and bundled, should I keep the "source.c" (or just the header file of "source.c") file included in the "BIG" include file that I will include in "main.c"? Will gcc recompile the "source.c" file? And how should I compile my main.c file? I saw two ways:

Like this:

gcc -o main main.c -L./ -ltest

Or like this:

gcc -o main main.c libtest.a

If I include "source.c" in the include file it compiles successfully, but how do I know that "source.c" is not getting recompiled?

EDIT: I have a source file (lib.c) I have everything included in it. (test.h and test.c) In test.h there's a function declaration (TestCall) and in test.c it's definition. Lib compiles successfully. But when I compile main.c with test.h included and libtest.a linked. Gcc throws "undefined reference to `TestCall'".

EDIT 2:

/*main.c*/
#include "test.h"

int main()
{
   TestCall();

   return 0;
}

/*test.h*/
#ifndef TEST_H
#define TEST_H

static void TestCall();

#endif

/*test.c*/
#include "test.h"

#include <stdio.h>

static void TestCall()
{
    printf("TestCall gets called!\n");
}

I tried a command: nm libtest.a

And it says TestCall is there:

test.o:
00000000 b .bss
00000000 d .data
00000000 r .eh_frame
00000000 r .rdata
00000000 r .rdata$zzz
00000000 t .text
     U _puts
00000000 t _TestCall
  • Both the commands you show are equivalent and will do the same thing. What is the problems you have with these commands? What is this "source.c" you talk about? Is it the source file for the static library? What is the purpose of the static library? Why do you need it in your project if there's only a single source file for the library? Will the library be reused by other projects? – Some programmer dude Jul 19 '21 at 07:40
  • 1
    What do you mean by "BIG include file"? You should never include source.c in other C files or header files. – Gerhardh Jul 19 '21 at 07:40
  • If you have a library, you should include the corresponding header with all declarations into your C file but you don't need the source as that is already in the lib. After all, that is the sole purpose of a lib. – Gerhardh Jul 19 '21 at 07:41
  • 1
    Don't include a BIG include... just include all the individual specific required headers one by one. – pmg Jul 19 '21 at 07:48
  • Related? https://stackoverflow.com/questions/8101576/is-it-right-to-simply-include-all-header-files – pmg Jul 19 '21 at 07:53

2 Answers2

0

If your static library project consists of 2 files "source.h" and "source.c" and you build a "libtest.a" from them with something like:

gcc -o source.o -c source.c
ar libtest.a source.o

then "source.c" is, obviously, already compiled. So you know that "source.c" isn't getting recompiled because "source.c" is not included in "libtest.a", only the compiled output "source.o" (the object file) is in your "libtest.a".

When using your libtest.a all you need to do is linking against libtest.a (by using -ltest) and having "source.h" in your include paths.

PS:
1.) never include *.c files.
2.) Please don't use "big" header files. Each header should have it's purpose (and that purpose shouldn't be "one header to rule them all")

Stefan Riedel
  • 796
  • 4
  • 15
  • Thanks everything, but it's still not working. – MedzsikTörtül Jul 19 '21 at 09:45
  • You shouldn't (as I said) include .c files. Never. So get rid of you "lib.c", you just have to compile "test.c". But that shouldn't be the problem here. Post the relevant pieces of code of all 3 files (test.h, test.c, main.c). And again, just to make that clear, DON'T INCLUDE .C FILES! – Stefan Riedel Jul 19 '21 at 14:28
0

So, I did some research and something caught my eye: "Unlike global functions in C, access to static functions is restricted to the file where they are declared." You can find the entire article here. Thanks for all of your answers.