-3

Edit #1: Please find the solution as an answer to this initial post as a code example is used. Please try also to find a possible solution in What is an undefined reference/unresolved external symbol error and how do I fix it? It didn't help me, but maybe it's suitable for you.

I try to build a project which consists of each two header and application files. If I compile each file, no errors occure. If I build the project, I run into the below error.

cd 'D:\Master\M_32561\9000_A\B13-03'
P:\PortableApps\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/P/PortableApps/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/Master/M_32561/9000_A/B13-03'
"/P/PortableApps/MinGW/msys/1.0/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/b13-03.exe
make.exe[2]: Entering directory `/d/Master/M_32561/9000_A/B13-03'
mkdir -p dist/Debug/MinGW-Windows
gcc     -o dist/Debug/MinGW-Windows/b13-03 build/Debug/MinGW-Windows/B13-03_F1.o build/Debug/MinGW-Windows/B13-03_MAIN.o 
build/Debug/MinGW-Windows/B13-03_F1.o: In function `anzeigen_artikelbestand':
D:\Master\M_32561\9000_A\B13-03/B13-03_F1.c:283: undefined reference to `ausgeben_artikelbestand_mit_listenkopf'
build/Debug/MinGW-Windows/B13-03_MAIN.o: In function `main':
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:39: undefined reference to `erfassen_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:42: undefined reference to `anzeigen_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:45: undefined reference to `aendern_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:48: undefined reference to `loeschen_artikel'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/b13-03.exe] Error 1
make.exe[2]: Leaving directory `/d/Master/M_32561/9000_A/B13-03'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/d/Master/M_32561/9000_A/B13-03'
make.exe": *** [.build-impl] Error 2

The project consists of the following files:

  1. B13-03_MAIN.h
  2. B13-03_MAIN.c
  3. B13-03_H1.h
  4. B13-03_F1.c

The undefined functions are declared in B13-03_MAIN.h and defined in B13-03_MAIN.c. Both application files are using B13-03_MAIN.h as a prototype.

Thanks for any help is much aprreciated.

IDE Version

Tools

Plugins

xp10r3r
  • 33
  • 5
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – HolyBlackCat Apr 15 '19 at 11:05
  • 1
    The functions mentioned as undefined, where are those defined (implemented)? Are they from a library? From some source file you've created? From a source file you *should* create? – Some programmer dude Apr 15 '19 at 11:06
  • @Someprogrammerdude The functions are declared in B13-03_MAIN.h as prototypes in B13-03_MAIN.c and B13-03_F1.c and defined in B13-03_MAIN.c. They aren't from a library. They 're from a source file like mentioned before. All source files belong to this example are created. – xp10r3r Apr 16 '19 at 09:03
  • Can you please try to create a [mcve] to show us? Not as links! Links can disappear or their contents change, making the question worthless (remember this site is not only to help you right now, but also others with similar problems in the future). Please try to make questions self-contained. Also please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 16 '19 at 09:16
  • @Someprogrammerdude I try to create an example to help others as well, of course after the issue is solved to focus on the important parts. I'll do it in my initial post. I was asking myself why I couldn't see options to upload the pictures in my initial post as I'm currently editing it. That's why I used links, and yes to help me in the first instance before helping others. It'd be benefitial to upload attachments to provide the complete code, however, it's unfortunately not supported on this side. – xp10r3r Apr 16 '19 at 10:16
  • I found the issue. Apparently, it has nothing to do with @HolyBlackCat posted dupe. The issue was caused by a wrong closing "}". It was implemented at the very end of B13-03_MAIN.c, i. e. the undefined functions were nested functions of main and not standalone functions. Thereby, I've another question: Why is the linker not able to build the project w/ this kind of error? The functions are declared within prototyp at the top of the program, but nested functions of main. They should be visible for the main function, because they're declared within the prototype, aren't they? – xp10r3r Apr 16 '19 at 10:17
  • And that's one of the reasons it's so important to create a [mcve] to show us. It can help you find the error yourself, and if not then we would have been able to find it without any guessing. – Some programmer dude Apr 16 '19 at 10:35
  • Also, I find it weird that the compiler didn't find such an error, or at least didn't provide a warning. Erroneous curly braces is something that should have been picked up earlier than linking. Perhaps you should enable more verbose warnings? At least `-Wall` and `-Wextra` (considering your use of GCC). – Some programmer dude Apr 16 '19 at 10:36
  • I mean from a syntax point of view everything should be fine. No missing curly braces, just incorrect nesting. I'm using MinGW as I'm on a Windows 10 machine. In the incorrect nesting version the undefined functions were declared and used before as well as defined within main function afterwards. They should be visible for the main function right after their declaration and able to use from that point onwards. They're defined later, as the error message said, but shouldn't they be visible globally? – xp10r3r Apr 16 '19 at 10:52

1 Answers1

0

The issue dealt with an incorrect nesting of the undefined functions. I focus on B13-03_MAIN.h and B13-03_MAIN.c.

/* B13-03_MAIN.h */
create_article();
show_article();
edit_article();
delete_article();

Implementation w/ issue:

/* B13-03_MAIN.c */
#include <stdio.h>
#include "B13-03_MAIN.h"
#include "B13-03_H1.h"

void main(void) { 
  switch(option) {
    case abc_c: create_article(); break;
    case abc_s: show_article(); break;
    case abc_e: edit_article(); break;
    case abc_d: delete_article(); break;
    default:    printf("Undefined option.");
  }

  void create_article(void) {
  ...
  }

  void show_article(void) {
  ...
  }

  void edit_article(void) {
  ...
  }

  void delete_article(void) {
  ...
  }
} /* This curly braces is incorrectly nested */

Implementation w/o issues:

void main(void) { 
  switch(option) {
    case abc_c: create_article(); break;
    case abc_s: show_article(); break;
    case abc_e: edit_article(); break;
    case abc_d: delete_article(); break;
    default:    printf("Undefined option.");
  }
} /*Incorrectly nested curly brace should be implemented here */

void create_article(void) {
...
}

void show_article(void) {
...
}

void edit_article(void) {
...
}

void delete_article(void) {
...
}
xp10r3r
  • 33
  • 5