Apologies if this question has been asked before, but all other threads are so old that it was advised to start a new thread.

As the title suggest, i'm having trouble linking files in Code::Blocks(Linux OS). I had the same problems back when i was using Windows. Error message is of undefined reference, even though i have the header file includes correct.

Example code:

main.c

#include "lib.h"

int main()
{
	printWorld();
	return 0;
}

lib.h

void printWorld();

lib.c

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

void printWorld()
{
	printf("Hello World!\n");
}

Is there a way to automatically link all include files, like what Mono is able to do. I would use Mono, but when it comes to accepting user input, it simply skips the steps and runs the program until it terminates, without taking user input.

Thanks in advance for any help with this.

Worked fine for me. You probably need to make all three files part of the project, see thumbnail.


>>Is there a way to automatically link all include files
No.

Thanks for helping out. I managed to get it to build all the files by right clicking project folder -> properties -> build target caret -> selecting the .c files. the .h files are linked automatically.

I wonder Ancient Dragonn, did you build using the default code::blocks settings with mingw compiler?

the .h files are linked automatically.

Headers aren't linked at all. Do you know about the primary phases of compilation?

  1. Preprocessor: Preprocessing directives are evaluated and textually replaced within the source code. Header files are expanded, macros are replaced, etc... The result is a translation unit.
  2. Compiler: Translation units are compiled into object files.
  3. Linker: Object files are linked together into an executable.

By the time the linker runs, the headers have already been expanded into the source files that include them, with those files compiled into object code.

I wonder Ancient Dragonn, did you build using the default code::blocks settings with mingw compiler?

Yes.

Headers aren't linked at all. Do you know about the primary phases of compilation?

Yeah, sorry about the misunderstanding there, it's a bad habit of mine of using one word for similar concepts.

Anyways, thanks for the replies guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.