Using function and header file (header)

2

I'm doing a simple program however using header file .h (header) and a hello world function, but I have the following alert message: Undefined reference to 'print' . I asked someone else to test it and said it worked out what it might still be?

Follow the code below:

=== Arquivo MainCodeCount.c ==

#include <stdio.h>
#include <stdlib.h>
#include "LibCodeCount.h"

int main(int argc, char *argv[])
{
    imprime();

    return 0;
}


=== Arquivo CodeCount.c ===

#include <stdio.h>
#include "LibCodeCount.h"

void imprime()
{
    printf("Ola mundo!\n");
}


=== Arquivo CodeCount.h (header) ==

void imprime();
    
asked by anonymous 02.04.2014 / 01:18

3 answers

3

The problem is merely that you compiled just one file, MainCodeCount.c and left CodeCount.c out. So there is no one setting the function the imprime() function in the linking step and the error occurs:

  

undefined reference to imprime()

The fix is to compile all your files:

gcc -c MainCodeCount.c                            (MainCodeCount.c -> MainCodeCount.o)
gcc -c LibCodeCount.c                             (LibCodeCount.c -> LibCodeCount.o)
gcc -o Hello MainCodeCount.o LibCodeCount.o       (*.o -> Hello.exe)

Another thing that is missing from your code is that the way it is is not going to cause you any problem, it's the header guards . In every header file use the following:

#ifndef MYHEADER_H
#define MYHEADER_H

// seu código aqui

#endif

Or:

#pragma once

// seu código aqui

The goal is for the header to be processed only once per compilation drive, even if it is included more than once.

    
02.04.2014 / 16:19
0

Try referencing all files

#include "CodeCount.h"
    
02.04.2014 / 01:24
-2

Try CodeCount.h, change the code to:

#ifndef __CODECOUNT_H__
#define __CODECOUNT_H__

void imprime();

#endif

In theory, your error message does not have to do with duplicate references, but I have no other ideas of what it can be now.

    
02.04.2014 / 01:32