Ubuntu Forums Archive Viewer

Problem with GCC/C/header file structure

Archived thread 2042323 from Ubuntu Application Development. Markdown source: Ubuntu_Application_Development/thread_2042323_Problem_with_GCCCheader_file_structure.md

Original URL About this archive
#1

This may or may not be a strictly Ubuntu question, but the competence to see through this problem is certainly found on this forum.

I have a problem with an exceptionally small C program whose only purpose is to secure that I have understood the principle of modularization through the use of header files.

I have one main program:

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

struct person
{
   char namn[20];
   int alder;
};

struct person p[5];

main()
{
   
   int i, aldst;
   for (i=0; i<5; i++)
   {
      p[i]=inperson();
   }
   aldst=0;
   for (i=0; i<5; i++)
   {
      if (p[i].alder>aldst)
      {
         aldst=i;
      }
   }
   utperson(p[aldst]);
}
   struct person inperson(void);
   void utperson(struct person p); 
#include <stdio.h>

struct person inperson(void)
{
   printf("Skriv namn och ålder med mellanslag: ");
   struct person p;
   scanf("%s %d",p.namn,&p.alder);
   return p;
}

void utperson (struct person p)
{
   printf("Personen %s, %d år, är äldst\n",p.namn,p.alder);
}

No amount of effort or restructuring keeps me from getting the error message

/tmp/cc7trJgi.o: In function main': testa_struct.c:(.text+0x34): undefined reference to inperson' testa_struct.c:(.text+0xdb): undefined reference to `utperson' collect2: ld returned 1 exit status

when I enter the text from $ below:

martin@martin-1001PX:~/C_programs$ gcc -o testa_struct.out testa_struct.c

WHAT

am I doing wrong? What is keeping GCC from finding this header file and the contained functions?

Any help would be more than appreciated, including pointers to COMPREHENSIVE tutorials on GCC and headers. The official Using the GNU Compiler Collection (GCC) may be very good but isn't helping anything with this specific problem.

#2

you need to list BOTH your .c files on the gcc command line I think

gcc -o testa_struct.out testa_struct.c funko_support.c
#3

steeldriver said: you need to list BOTH your .c files on the gcc command line I think

gcc -o testa_struct.out testa_struct.c funko_support.c

Ah. Several directions to try to move in. Thanks! This place is bustling with experts like you. :)

And most impressive of all is the speed at which experts move in. Quick help is double help.