Pergunta

I am using clang compiler and trying to separate my files into header files, however there are two problems either the struct isn't visible or there are a bunch of function prototypes that are never included.

header.h

#ifndef GUARD
#define GUARD 1

struct A{
    int b;

};
// some function prototypes

int blah(struct A* a);

#endif

I then have an implementation of it

header.c

and a sample file to use it that includes the header however I get several errors of either

Undefined symbols for architecture x86_64:
  "_blah", referenced from:
      _main in main-Ghk6wa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

or if I remove the struct from the file reports of the struct not being visible and errors.

I then have the inclusion of the header file in the implementation and the sample test file

Foi útil?

Solução

You must certainly link your whole program together, or you will certainly have lots of unresolved symbols. One way to achieve that is feeding all source files to the compiler at once, the other is compiling to object files and then doing a final linking step.

Outras dicas

A header file is not a library. You need to include the header in your source files so the compiler knows what the structures and functions look like, but when it comes to building the program, you need to compile both the code that implements the library functions and the code that uses them.

Often you'll build the library just once and then use it many times. This is what happens with the C library, for example.

So, in the simplest case, you might use:

gcc -o program program.c header.c
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top