C programming and "include"

KataKoniK|ATI

New member
Hi,

Just say your code looks like the following:

Code:
#include <stdio.h>
#include <myownprogram.h>

code goes here
code goes here

When you compile the program, the compiler will say that it cannot find "myownprogram.h". Therefore, I was wondering how would you link "myprogram.c" to the #include <myownprogram.h> in order to solve this problem (get rid of the compiler message)? Hope this makes sense. Thanks.
 
KataKoniK|ATI said:
Hi,

Just say your code looks like the following:

Code:
#include <stdio.h>
#include <myownprogram.h>

code goes here
code goes here

When you compile the program, the compiler will say that it cannot find "myownprogram.h". Therefore, I was wondering how would you link "myprogram.c" to the #include <myownprogram.h> in order to solve this problem (get rid of the compiler message)? Hope this makes sense. Thanks.

#include "myownprogram.h"

try that...
 
That worked, thanks. Another question though, does #include "myownprogram.h" ensure that it is linked with myownprogram.c?

I'm slowly adapting to C. There's quite a few things in this language that's different from Java.
 
KataKoniK|ATI said:
That worked, thanks. Another question though, does #include "myownprogram.h" ensure that it is linked with myownprogram.c?

I'm slowly adapting to C. There's quite a few things in this language that's different from Java.
No, it doesn't ensure that. In fact, you could have different filenames for the header and the source files (though that would be weird).

Header files are just for the compiler to know how to call certain functions, but don't include the function definitions themselves (only declarations).

Each source (*.c) files should be compiled, and then the resulting object files have to be linked together to get an executable.
 
Hmm, that makes sense. Though, can you tell me what code to use to ensure that #include "myownprogram.h" is linked with myownprogram.c? Is it extern?
 
Last edited:
You can't link header files to C source files. I assume you also have a C file called "myownprogram.c" containing all the function definitions, global variable definitions, etc.

The whole process of compiling the source code to an exe depends on the compiler, but the idea is the same as in my previous post. What compiler/linker do you use?

Here's what header files should contain:
- function declarations (int myFunction(int))
- global variable declarations (extern int blahblah; )
- preprocessor directives (#include, #define, #ifdef, #ifndef etc.)

Here's what C source files should contain:
- preprocessor directives (#include, #define, #ifdef, #ifndef etc.)
- function definitions ( int myFunction(int a) { return a+1; } )
- global variable definitions (int blahblah = 2; )
- static variables and functions that are global but cannot be
accessed from outside the C file (static int foobar = 99; )

You can find some good tutorials and guides on oopweb.com.
 
Last edited:
KataKoniK|ATI said:
Hmm, that makes sense. Though, can you tell me what code to use to ensure that #include "myownprogram.h" is linked with myownprogram.c? Is it extern?
It's not linked in.

#include is a pre-processor directive. That means it's done before the compiler and linker.

When the pre-processor sees #include it copies the entire contents of the file into the .c file. In fact, you could #include a .c file if you wanted too.

Check out the options for your compiler. Get it to output the processed files (after pre-processor, before compiler). You'll see the #define substitutions and includes put into the source.

BTW, #include <file.h> tells it to look in the include path, #include "file.h" tells it to look for the file relative to the file it's in now.

Hope this helps.
 
Yeah, that makes sense. Thanks. The reason I am asking if #include "myownprogram.h" links to myownprogram.c is because mop.h has the line:

extern char *myarray[10][10];

In mop.c, the contents of char *myarray[10][10] is defined (and has defined, #include "myownprogram.h"). For example, *myarray[0][1] = "5" and etc. The problem I am having is, how would I print out char *myarray[5][5] for example in my current program (that has defined #include "mop.h") I hope this makes sense.

Thanks in advance.
 
Last edited:
Let's see if I understand right:

You have an array declared in some.c, extern defined in some.h and you want to use it in someother.c?

If this is what you're aksing then you need to compile both .c's and link them together. Right now you may be compiling and linking in one step. Make the compile command ONLY compile the two .c's and then link them togther.
 
Hmm, I understand what you mean, but I don't know how to do that. I read a few tutorials on this issue and realized that I can just type the name of the array ie. myarray and do stuff with it in the file that has #include "myownprogram.h". However, it will not let me print out the contents of it (testing purposes) as it says that myarray is an undefined reference when I try to do the following

Code:
int i, j;

for(i = 0; i < 10; i++) {
  for(j = 0; j < 10; j++) {
    printf("%c", myarray[i][j]);
  }
}

http://en.wikipedia.org/wiki/Header_file
 
Last edited:
what's your header file?

this is my code and it works under gcc v4

test.h
Code:
#if !defined(TEST_H)
#define TEST_H

int myarray[10][10];
#endif

and test.c
Code:
#include "test.h"
#include <stdio.h>

int i,j;
int main(void){
        for(i=0;i<10;++i) for(j=0;j<10;++j) myarray[i][j]=i+j;
        for(i=0;i<10;++i){
                for(j=0;j<10;++j) printf("%-3d",myarray[i][j]);
                printf("\n");
        }
        return 0;
}
 
Last edited:
Hmm, this is the error I am getting:

test.h

Code:
extern char *myarray[10][10];

test.c
Code:
#include "test.h"
char *myarray[10][10] = {{ elements inserted here }};

myprogram.c

Code:
#include "test.h"
#include <stdio.h>
int main(void) {

  int i, j;

  for(i = 0; i < 10; i++) {
    for(j = 0; j < 10; j++) {
      printf("%c", myarray[i][j]);
    }
  }
  return 0;
}

^^ undefined reference to myarray (caught by compiler in myprogram.c)
 
Last edited:
KataKoniK|ATI said:
Hmm, this is the error I am getting:

test.h

Code:
extern char *myarray[10][10];

test.c
Code:
#include "pic.h"
char *myarray[10][10] = {{ elements inserted here }};

myprogram.c

Code:
#include "test.h"
#include <stdio.h>
int main(void) {

  int i, j;

  for(i = 0; i < 10; i++) {
    for(j = 0; j < 10; j++) {
      printf("%c", myarray[i][j]);
    }
  }
  return 0;
}

^^ undefined reference to myarray (caught by compiler in myprogram.c)


I'm not saying you can't do this, but why?

Just curious....
 
KataKoniK|ATI said:
Hmm, this is the error I am getting:

test.h

Code:
#if !defined(__TEST_H)
#define __TEST_H
char *myarray[10][10];
#endif

test.c
Code:
#include "pic.h"
#include "test.h"
*myarray[10][10] = {{ elements inserted here }};/*this will fail, replace with correct initialization code*/

myprogram.c

Code:
#include "test.h"
#include <stdio.h>
int main(void) {

  int i, j;

  for(i = 0; i < 10; i++) {
    for(j = 0; j < 10; j++) {
      printf("%c", myarray[i][j]);
    }
  }
  return 0;
}

^^ undefined reference to myarray (caught by compiler in myprogram.c)
do it that way. but what you are doing seems so trivial. why not just roll it into 1 file? if you must use extern, here is another way to do it:

Code:
#if !defined(__TEST_H)
#define __TEST_H
char *myarray[10][10];
#endif

test.c
Code:
#include "pic.h"
#include "test.h"
*myarray[10][10] = {{ elements inserted here }}; /*this will fail, replace with correct initialization code*/

myprogram.c

Code:
#include <stdio.h>
extern char *myarray[10][10];
int main(void) {
  int i, j;

  for(i = 0; i < 10; i++) {
    for(j = 0; j < 10; j++) {
      printf("%c", myarray[i][j]);
    }
  }
  return 0;
}
 
Last edited:
No.

Your syntax is fine.

Where the extern define goes is a matter of style/preference. The problem is a linking problem.

When you define a var extern you are telling that file (myprogram.c) that a variable exists in the project called 'myarray'. but when the project is linked it can't resolve the variable name because your other c file (test.c) is not being compiled.

As some are saying, you could put it all in one file and solve the problem that way. BUt since you are learning C, you should take the oppurtunity to learn how to link multiple files together and create a multi-file project as opposed to a single-file program.

So, let's take a step back.

What environment/compiler are you using?
 
I am not sure what compiler I'm using, but to compile the program on my school's server, I have to type

gcc -Wall filename.c

Is there a way to check?

Thanks.
 
KataKoniK|ATI said:
I am not sure what compiler I'm using, but to compile the program on my school's server, I have to type

gcc -Wall filename.c

Is there a way to check?

Thanks.
<3 gcc

Make a batch file to do your builds if you have more than a single c file. The header files will be linked during compilation but with a build file you can set it such that all c files in a directory are included or you can list only certain c files you want built in the solution. Look up makefiles and how to build your own. This will be much better than typing out the gcc command over and over again. GL! :D

edit:
here is an example that I am using. dont use it because there is stuff here you wont be using. just focus on the first couple lines.

build.bat
Code:
@echo off
echo  compiling...
bin\m6811-elf-gcc -m68hc12 -g -O1 -mshort -Wl,-m,m68hc12elfb -o obj\main.elf src\*.c
ECHO.
@if errorlevel 1 goto error1
bin\m6811-elf-objcopy --only-section=.text --only-section=.rodata --only-section=.vectors --only-section=.data --output-target=srec obj\main.elf obj\main.s19
bin\m6811-elf-objdump -d -S obj\main.elf > obj\main.ds
bin\m6811-elf-objdump -x obj\main.elf > obj\main.dx
bin\m6811-elf-nm.exe -C -l -n obj\main.elf > obj\main.nl
bin\m6811-elf-nm.exe -C -l --size-sort obj\main.elf > obj\main.ns
bin\m6811-elf-readelf.exe -a obj\main.elf > obj\main.eld
bin\gnunsym obj\main.nl obj\-Smains.noi obj\-Emaine.noi
ECHO.
ECHO  Done.
goto finish
:error1
ECHO    ** There Are Errors **
:finish
ECHO  press any key to continue...
PAUSE >NUL
 
Last edited:
So it's gcc...good.

notice this line in Zenitram's build file:
bin\m6811-elf-gcc -m68hc12 -g -O1 -mshort -Wl,-m,m68hc12elfb -o obj\main.elf src\*.c

He's using a gcc version for the m6811 platform (Motorola??)
Some of his options can be ignored, but let's look at a few:
-g -- enables debug symbols. Needed to use a debugger.
-O1 --optimization level. Don't need to worry about it.
-o is your output file. By defualt I think it outputs a.out (on linux at least). use this option with 'myprog' or 'test' or whatever you want your executable to be called.
'src\*.c' is the files he's passing in to be compiled and linked together.

The rest of the options are of no consequence right now or are for his target platform (the aforementioned m6811). The same goes for the rest of his commands in the build file.

My guess, Zenitram is that you're working on an embedded processor. Right?

If you type gcc --help you'll see some of the options. I suspect you're running this on a Linux server, so you should also be able to type 'man gcc' to see the manual.

Yout can read up on options like I mentioned before:
-c to comile the files only not link them, then link them in a second step.
-E to pre-process them only.

Let us know how it goes.
 
ugh paramter overload. if you're just starting out, just know

gcc -Wall file1.c file2.c file3.c ... fileN.c

compiles file1,file2,file3,...,fileN and links them together into the executable a.out.

as for style... last time I checked every c file loads in its header. It's rather pointless to call a file func.h, and then not have a file func.c load it. if you're managing large amounts of code it'll confuse the hell out of the person who might be replacing you.
 
Back
Top