Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: Dexters Lab on September 09, 2013, 01:45:14 am

Title: Issues with linker: header files
Post by: Dexters Lab on September 09, 2013, 01:45:14 am
Hello,

I'm new to code::blocks. I program in C and use Linux Mint as my environment.
I have to say that I'm loving code::blocks so far!

However, for some time I'm having issues with linking my main program to libraries, getting "undefined reference to <function>". In searches I've been making to look for solving my problem I realize that I'm not the only one. But I didn't find any answer that could be applied here.

This is my code with the three of my files:

maintest.c
Code
#include <stdio.h>
#include "lib.h"

int main() {

menu(3, "one", "two", "three");

return 0;
}

lib.h
Code
#ifndef _LIB_H_
#define _LIB_H_
typedef char* string;

void menu(int count, ...);

#endif

lib.c
Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "lib.h"

void menu(int count, ...) {

    va_list listPointer;

    va_start(listPointer, count);

    for(int i = 1; i <= count; i++) {
        char *string = va_arg(listPointer, char*);

        printf("%d ..... %s", i < count ? i : 0 , string);
    }
    va_end(listPointer);
}

The compiler (gcc) does ok if I compile this by terminal with the following command:
gcc -Wall maintest.c lib.c -o result

But if I just compile maintest.c, it happens that the same error occurs of "undefined reference to menu()".

I would like to know why code::blocks isn't compiling both files as it should, since one is calling another.
I'm really loving this IDE. I used Dev-C++, Borland and kdevelop, and this is by far the best. Although I'm having this issue that I never had before, so for that reason I'm unexperienced with linker problems.

I hope I'm on right section and sorry if this kind of question are obsolete, it's just I couldn't find an answer.

Hope a solution soon as possible, since I'm starting school and really need this ready.
Thank you very much for your time.
Title: Re: Issues with linker: header files
Post by: stahta01 on September 09, 2013, 03:14:34 am
1. Create a CB Project
2. Add the source files to the project.

If it still does not work.
Read these FAQs
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F)
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F)

Edit: Also, this post most likely belonged in this sub-forum http://forums.codeblocks.org/index.php/board,3.0.html (http://forums.codeblocks.org/index.php/board,3.0.html)

Tim S.

Title: Re: Issues with linker: header files
Post by: Dexters Lab on September 09, 2013, 11:10:45 am
Thank you for your answer.

But why does code::blocks only allows us to compile files that are in the same project?
Because I don't like to make projects, I do prefer empty files from the scratch. I'm used to program in text editors and compile from the command line so I would dispense this project thing, and this is why I like CB so much: it's simple and we can configure it to be almost a text-editor with all functionalities of an IDE.

How can I still reach this without having to make projects?
Title: Re: Issues with linker: header files
Post by: oBFusCATed on September 09, 2013, 11:13:29 am
How can I still reach this without having to make projects?
Just use a text editor and command line, not an IDE!
If you want to have two+ source files (translation units) you'll have to make a project.
This is how it works in C::B or any other IDE.
Title: Re: Issues with linker: header files
Post by: Dexters Lab on September 09, 2013, 09:40:52 pm
How can I still reach this without having to make projects?
Just use a text editor and command line, not an IDE!
If you want to have two+ source files (translation units) you'll have to make a project.
This is how it works in C::B or any other IDE.

Sir, it was possible in Dev-C++ to do it. I'm going to stick with C::B because I really like it but it's too bad that is not possible to link files without making a project.

I appreciate your time.
Title: Re: Issues with linker: header files
Post by: dmoore on September 09, 2013, 10:25:28 pm
If you don't use a project, how is the compiler supposed to know what files to build and link? e.g. what if you had many versions of lib.c in the directory? If you don't want to use a project, you can always #include "lib.c" in your maintest.c but that's pretty ugly if you move beyond just an extra file or two.

That said I feel your pain about the "cost" of creating a new project. It would be nice if there was a menu item like "New Project from sources..." that prompts you to choose a set of sources in a file picker, suggests a name based on the directory where the files are located, and then creates a single target with those sources. (and similarly "New Target from sources...")