Two main questions about compilation and libraries

I have two half-asked questions.

My first question is: I can call functions in the standard library without compiling the entire library simply:

#include <stdio.h>

How will I do the same with my header files? Just “including” my plaintext header files obviously doesn't work.

#include "nameofmyheader.h"

Basically, how can I create a library that other files can call?

Second question: suppose I have a program broken into 50 c files and a header file. What is the correct way to compile it:

cc main.c 1.h 1.c 2.c 3.c 4.c 5.c 6.c 7.c   /*... and so on*/

Please correct any errors that I have. I am completely lost here.

+3
source share
5 answers

-, , #include. . (DLL .lib Windows,.a .so Linux). , #include, , .

, #include, , . Java Python, #includes . " ", , . #include in C : " , ". , #include <stdio.h> , - . , , , .

, . , ( ) (DLL .so), (.lib .a) . "" , .

, , , , , , .

. , , . . , , foo.c, foo.h. - , "extern". , foo.c

int some_global;

void some_function(int a, char b)
 {
     /* Do some computation */
 }

, , foo.h

extern int some_global;

void some_function(int, char);

#include "foo.h", some_global some_function. , " ", . , foo.h :

#ifndef FOO_H
#define FOO_H

extern int some_global;

void some_function(int, char);

#endif

, ( ).

, .h , ( ).

cc main.c 1.c 2.c 3.c ... [etc]

, 50 , , , , . Linux Makefile. Windows , . Google SO, ( ).

, , , , , ( ), , , , . , .

+14

, - , .

, .h -lyourlib, libc

, :

gcc -shared test.c -o libtest.so

:

gcc myprogram.c -ltest -o myprogram

Makefile http://www.gnu.org/software/make/

0

, , .h, "" .h. - .h .c . , , - "".

0

.c . .c , .

make .

makefile . , .

0

, . , . , /.

, . () .

, #include <stdio.h> IMPORT . (, , , , ..).

You can split your files into modules and create shared libraries. But, as a rule, as projects grow, you usually need the best mechanism for creating your program (and libraries). Instead of calling the compiler directly when you need to rebuild, you should use the make program or a complete build system such as the GNU Build System .

0
source

Source: https://habr.com/ru/post/1716193/


All Articles