Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
Issues with linker: header files
Dexters Lab:
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;
}
--- End code ---
lib.h
--- Code: ---#ifndef _LIB_H_
#define _LIB_H_
typedef char* string;
void menu(int count, ...);
#endif
--- End code ---
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);
}
--- End code ---
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.
stahta01:
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_%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
Tim S.
Dexters Lab:
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?
oBFusCATed:
--- Quote from: Dexters Lab on September 09, 2013, 11:10:45 am ---How can I still reach this without having to make projects?
--- End quote ---
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.
Dexters Lab:
--- Quote from: oBFusCATed on September 09, 2013, 11:13:29 am ---
--- Quote from: Dexters Lab on September 09, 2013, 11:10:45 am ---How can I still reach this without having to make projects?
--- End quote ---
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.
--- End quote ---
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.
Navigation
[0] Message Index
[#] Next page
Go to full version