Undefined reference problem

Hello everyone,

i'm having a problem related to the undefined reference error.

The function below return an error during make like: undefined reference to 'lightpen_enabled'.

Now, all the 3 parameters of this conditional check if (_mouse_enabled && !lightpen_enabled && !sdl_menu_state) are defined into respective file, lightpen.h, uimenu.h and mouse.h, each as follow:

1
2
3
extern int _mouse_enabled;
extern int lightpen_enabled;
extern int sdl_menu_state;


why i get only the error undefined reference to 'lightpen_enabled'?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "lightpen.h"
#include "uimenu.h"
#include "mouse.h"



void ui_check_mouse_cursor(void)
{
    if (_mouse_enabled && !lightpen_enabled && !sdl_menu_state) {
#ifndef USE_SDLUI2
        SDL_ShowCursor(SDL_DISABLE);
        SDL_WM_GrabInput(SDL_GRAB_ON);
#else
        SDL_SetRelativeMouseMode(SDL_TRUE);
#endif
    } else {
#ifndef USE_SDLUI2
        SDL_ShowCursor((sdl_active_canvas->fullscreenconfig->enable && !lightpen_enabled) ? SDL_DISABLE : SDL_ENABLE);
        SDL_WM_GrabInput(SDL_GRAB_OFF);
#else
        SDL_SetRelativeMouseMode(SDL_FALSE);
#endif
    }
}


Last edited on
Hello Amiplus,

Post the exact error message, which is good practice to start with, someone may see something that you do not.

Check your spelling for "lightpen_enabled". It could be misspelled somewhere you did not notice.

Not all error messages will tell you where the problem is just where it showed up. Many times the actual error is above the line picked as having an error. Or in this case it could be in another file.

Right now I do not see enough of the program to be able to trace "lightpen_enabled" from its original definition to the code above.

Hope that helps,

Andy
Thank you. I'll try to post the needed files asap

"undefined reference" is a linker error.

You have already compiled this file that implements the void ui_check_mouse_cursor) (and possibly some other translation units. In this file (via include) it is known that somebody else implements the int lightpen_enabled.

Now linker should put the binary together. None of the object files that you have given to the linker does contain the implementation of int lightpen_enabled.

In other words, your build system does not handle all necessary files.
@keskiverto
Thank you the reply.

Reading some explanation about the error above, i notice some similar words.

How can i instruct my build system to handle the necessary files?

here's a more detailed output of the error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Making all in platform
main.o: In function `main_program':
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/main.c:235: undefined reference to
`core_team'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/main.c:237: undefined reference to
`core_team'
../src/arch/sdl/libarch.a(ui.o): In function `ui_check_mouse_cursor':
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/ui.c:496: undefined
reference to `lightpen_enabled'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/ui.c:496: undefined
reference to `lightpen_enabled'
../src/arch/sdl/libarch.a(lightpendrv.o): In function `sdl_lightpen_update':
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/lightpendrv.c:47:
undefined reference to `lightpen_enabled'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/lightpendrv.c:47:
undefined reference to `lightpen_enabled'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/lightpendrv.c:95:
undefined reference to `lightpen_update'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/lightpendrv.c:95:
undefined reference to `lightpen_update'
../src/arch/sdl/libarch.a(menu_help.o): In function `about_callback':
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/menu_help.c:368: undefined
reference to `core_team'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/menu_help.c:368: undefined
reference to `core_team'
../src/arch/sdl/libarch.a(menu_help.o): In function `contrib_convert':
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/menu_help.c:137: undefined
reference to `info_contrib_text'
/AOS/Software/Development/Vice
31/vice-am-31/vice-3.1/src/arch/sdl/menu_help.c:137: undefined
reference to `info_contrib_text'
gmake[3]: *** [vsid] Error 1
gmake[2]: *** [all-recursive] Error 1
bin:gmake[1]: *** [all] Error 2
gmake: *** [all-recursive] Error 1
3.AOS:Software/Development/Vice 31/vice-am-31/vice-3.1> 
Where do you actually define lightpen_enabled and all those other global variables?

You've showed us where you declare them (as externs), but you haven't shown us where you actually define them. There needs to be a single definition somewhere.

Last edited on
Now, all the 3 parameters of this conditional check if (_mouse_enabled && !lightpen_enabled && !sdl_menu_state) are defined into respective file, lightpen.h, uimenu.h and mouse.h, each as follow:

1
2
3
extern int _mouse_enabled;
extern int lightpen_enabled;
extern int sdl_menu_state;


in mouse.c, lightpen.c and sdl_menu_state.c are initialized.

this is initialized into lightpen.c

int lightpen_enabled = 0;

keskiverto wrote:
your build system does not handle all necessary files.

I think this could also mean you don’t list all the source files in your compilation command.
Could you please check if they are all there? Something like:
gcc ... mouse.c lightpen.c sdl_menu_state.c main.c ... -o myprog.exe

Also, is your code entirely in C or is it a mixture of C and C++? In that case, maybe you need the extern C declaration.

...are defined into respective file...
1
2
3
extern int _mouse_enabled;
extern int lightpen_enabled;
extern int sdl_menu_state;

As far as I know, those are declarations, not definitions.
About the declarations, my mistake, sorry.

Configure files and makefiles are huge. I will try to search into those, but i suspect that compilations of various files is made using some sort of wildcard.

thank you
Configure files and makefiles are huge.

Makefiles as in many files?


@Enoizat
The lowest level of compiling is to manually call gcc/g++ with some options.

The next level is to write a Makefile manually and run make/gmake/nmake.

The next level is to use some tool (e.g. GNU autotools, cmake, qmake) that automatically writes the Makefile(s) for you from "project config". These tools are likely to have a method to generate the initial config.

The next level is an IDE, where one can clickety clack, GUI crap the project config, makefile generation, compilation, etc. Due to the nature of GUI, wildcards are highly unlikely in IDE.
keskiverto wrote:
wildcards are highly unlikely in IDE

That’s a super-canny remark, keskiverto.
Makefile in many directories.

The project is not mine. It's an already made software i'm trying to compile on an exotic system.

So, configure is already made. I can pass to it some parameters to adapt to my system, but of course there are some limitations.

Trying to figure out how to solve the errors step by step.
Topic archived. No new replies allowed.