Author Topic: Issues with linker: header files  (Read 18549 times)

Offline Dexters Lab

  • Single posting newcomer
  • *
  • Posts: 3
Issues with linker: header files
« 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.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7785
    • My Best Post
Re: Issues with linker: header files
« Reply #1 on: September 09, 2013, 03:14:34 am »
« Last Edit: September 09, 2013, 04:41:55 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Dexters Lab

  • Single posting newcomer
  • *
  • Posts: 3
Re: Issues with linker: header files
« Reply #2 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?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: Issues with linker: header files
« Reply #3 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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Dexters Lab

  • Single posting newcomer
  • *
  • Posts: 3
Re: Issues with linker: header files
« Reply #4 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.

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Issues with linker: header files
« Reply #5 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...")