2

Using a very simple source and header file setup (main.cpp, callbacks.h and callbacks.cpp) I get an error message saying: Undefined reference to "error_callback(int, char const*)". It seems some have run into the same problem, but not with quite as simple circumstances, which is what has really thrown me off.

Here are the files:

main.cpp

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

int main(void){
    char er[] = "Error.";
    error_callback(1, er);
    return 0;
}

callbacks.h:

#ifndef CALLBACKS_H
#define CALLBACKS_H

void error_callback(int error, const char* description);

#endif // CALLBACKS_H

and finally callbacks.cpp:

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

void error_callback(int error, const char* description){
    fprintf(stderr, "Error: %s\n", description);
}

I cannot possibly imagine a simpler header file setup - yet something's gone wrong. I'm 99.9% sure I've managed the header and source files correctly.

EDIT

It seems it was an issue with the IDE Code Blocks. Source files will show in the directories even if they are not properly linked in the project. Manually adding every file again resolved the issue.

Felix
  • 2,548
  • 19
  • 48
  • 2
    Did you add `callbacks.cpp` to the files that need to be compiled? – NathanOliver Sep 11 '17 at 12:36
  • 2
  • If you used GCC then compiled Like this: g++ main.cpp callbacks.cpp – msc Sep 11 '17 at 12:39
  • 5
    Show the command line command you use to build the code. – Angew is no longer proud of SO Sep 11 '17 at 12:42
  • Maybe CALLBACKS_H has defined somewhere. Try different define like ____CALLBACKS_H____ – pat Sep 11 '17 at 12:49
  • 5
    @PhạmAnhTuấn - Very bad suggestion. Identifiers starting with a double underscore or an underscore followed by a capital letter are **all reserved** for the implementation. – StoryTeller - Unslander Monica Sep 11 '17 at 12:59
  • Oh, sorry didn't know that. Just want to sugest him to try different indentifier. MY_CALLBACKS_H – pat Sep 11 '17 at 13:04
  • @PhạmAnhTuấn that's an OK header guard. Still, I think it is unlikely the problem because we can clearly see from the code that `CALLBACKS_H` is not declared anywhere else. Only file that is not shown is `` which is guaranteed to not define `CALLBACKS_H`. Only possibility left is that Felix has explicitly asked the compiler to define it. – eerorika Sep 11 '17 at 13:10
  • Perhaps [Run a program with more than one source file in GNU c++ compiler](https://stackoverflow.com/questions/11155327/run-a-program-with-more-than-one-source-files-in-gnu-c-compiler) will help. – Bo Persson Sep 11 '17 at 13:15
  • It was an issue with the IDE. Check the edit for details. What a "feature". – Felix Sep 11 '17 at 13:30
  • But thank you for the input everyone! I should just trust the errors. – Felix Sep 11 '17 at 13:31
  • 1
    @Felix Feel free to write an answer to your own question explaining the issue and accepting it. [Self-answering is encouraged here](https://stackoverflow.com/help/self-answer)! – ComicSansMS Sep 11 '17 at 13:32

0 Answers0