multiple definition

M

Michael

Hi
while trying to compile a project I get tons of:

multiple definition of `blalb`

how to approach such within hundreds? of files?
Many thanks
Michael
 
V

Victor Bazarov

while trying to compile a project I get tons of:

multiple definition of `blalb`

how to approach such within hundreds? of files?

One by one. Or divide an conquer.

Most likely you have function definitions or object definitions in your
headers. Don't. Move object definitions to a separate C++ file and
compile it separately, and add 'inline' to any function definition in
the header that is outside a class definition.

V
 
M

Michael

the api_acf.h file:

#ifndef __API_ACF_H__
#define __API_ACF_H__


int gizmo_builtin_acf_getAuthor(lua_State *L);
int gizmo_builtin_acf_getAltAgl(lua_State *L);
....


and in api_acf.cpp:

/*
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "/usr/include/lua5.1/lua.h"
#include "/usr/include/lua5.1/lauxlib.h"
#include "../../../SDK/CHeaders/XPLM/XPLMDataAccess.h"
#include "../gizmo_global_datarefs.h"
#include "../XPlaneHooks.h"
#include "api_acf.h"

int gizmo_builtin_acf_getAltAgl(lua_State *L){ lua_pushnumber(L,
XPLMGetDataf( gizmo_xpdr_AGL )); return 1; }; //meters
....

where I get:
multiple definition of `gizmo_builtin_acf_getAltAgl(lua_State*)'

Why, what could be my fault?
Thanks
 
V

Victor Bazarov

the api_acf.h file:

#ifndef __API_ACF_H__
#define __API_ACF_H__

DO NOT use any identifiers with double underscores in them. What is
your reason to name that macro "__API_ACF_H__"? Why not simply
"API_ACF_H_INCLUDED"?
int gizmo_builtin_acf_getAuthor(lua_State *L);
int gizmo_builtin_acf_getAltAgl(lua_State *L);
...


and in api_acf.cpp:

/*
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Looks like it's not your code, and you have downloaded it from
somewhere. Make sure you don't put both the source and the object
module (like a library) in your linker command line.
*/

#include "/usr/include/lua5.1/lua.h"
#include "/usr/include/lua5.1/lauxlib.h"
#include "../../../SDK/CHeaders/XPLM/XPLMDataAccess.h"
#include "../gizmo_global_datarefs.h"
#include "../XPlaneHooks.h"
#include "api_acf.h"

int gizmo_builtin_acf_getAltAgl(lua_State *L){ lua_pushnumber(L,
XPLMGetDataf( gizmo_xpdr_AGL )); return 1; }; //meters
...

where I get:
multiple definition of `gizmo_builtin_acf_getAltAgl(lua_State*)'

Why, what could be my fault?

You could be linking your object file twice (check your make file, if
you have any), you could be #include'ing your .cpp file where you
shouldn't... I don't know. Not enough information.

V
 
M

Michael

Victor I get f.ex lots alike:

(.bss+0x64e0):-1: Fehler:multiple definition of
`gizmo_flag_LuaRestartCalled'

and searching in the source returns:

michael@ubuntu:~/CPP/Gizmo$ grep -R "gizmo_flag_LuaRestartCalled" .
../plugin_source/gizmo_lua_api/api_gizmo.cpp: gizmo_flag_LuaRestartCalled
= true;
../plugin_source/XPlaneHooks.h:bool gizmo_flag_LuaRestartCalled = false;
../plugin_source/XPlaneHooks.cpp: if( gizmo_flag_LuaRestartCalled ){
../plugin_source/XPlaneHooks.cpp: gizmo_flag_LuaRestartCalled = false;
../plugin_source/XPlaneHooks.cpp: gizmo_flag_LuaRestartCalled = true;
../plugin_source/XPlaneHooks.cpp: gizmo_flag_LuaRestartCalled = true;
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_mouse.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_gui.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_trig.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_matrix.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_acf.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_camera.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_physics.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_http.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_timer.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_fmc.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_shaders.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_gizmo.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_xp.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_particles.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_memcache.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_gfx.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_hash.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_plugins.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_menu.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_sound.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_nav.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_logging.o.
Übereinstimmungen in Binärdatei
../Debug/plugin_source/gizmo_lua_api/api_utils.o.
Übereinstimmungen in Binärdatei ./Debug/plugin_source/xlua_builtins.o.
Übereinstimmungen in Binärdatei ./Debug/plugin_source/XPlaneHooks.o.
Übereinstimmungen in Binärdatei ./gizmo-build-desktop/obj/XPlaneHooks.o.
Übereinstimmungen in Binärdatei
../gizmo-build-desktop/obj/BulletPhysicsHooks.o.
michael@ubuntu:~/CPP/Gizmo$



Many thanks for your help.
Regards Michael
 
V

Victor Bazarov

Victor I get f.ex lots alike:

(.bss+0x64e0):-1: Fehler:multiple definition of
`gizmo_flag_LuaRestartCalled'

and searching in the source returns:

michael@ubuntu:~/CPP/Gizmo$ grep -R "gizmo_flag_LuaRestartCalled" .
./plugin_source/gizmo_lua_api/api_gizmo.cpp: gizmo_flag_LuaRestartCalled
= true;
./plugin_source/XPlaneHooks.h:bool gizmo_flag_LuaRestartCalled = false;

This looks like a definition of a variable (with an initializer, no
less). A header is just a *very wrong place* for it. Is this your
code? If not, are you including that header in more than one C++ file?
If the latter, don't. Include it in only one C++ module, otherwise
you're going to have the multiple definitions. If it's your code, *move
the definition* to a C++ module. See FAQ.
./plugin_source/XPlaneHooks.cpp: if( gizmo_flag_LuaRestartCalled ){
./plugin_source/XPlaneHooks.cpp: gizmo_flag_LuaRestartCalled = false;
./plugin_source/XPlaneHooks.cpp: gizmo_flag_LuaRestartCalled = true;
./plugin_source/XPlaneHooks.cpp: gizmo_flag_LuaRestartCalled = true;
Übereinstimmungen in Binärdatei

Sorry, my German is rusty. Does that mean it found it in those .o files?
./Debug/plugin_source/gizmo_lua_api/api_mouse.o.
Übereinstimmungen in Binärdatei
./Debug/plugin_source/gizmo_lua_api/api_gui.o.
Übereinstimmungen in Binärdatei
[...]



Many thanks for your help.
Regards Michael

V
 
S

SG

Victor I get f.ex lots alike:

(.bss+0x64e0):-1: Fehler:multiple definition of
`gizmo_flag_LuaRestartCalled'

and searching in the source returns:

michael@ubuntu:~/CPP/Gizmo$ grep -R "gizmo_flag_LuaRestartCalled" .
[--snip--]
./plugin_source/XPlaneHooks.h: bool gizmo_flag_LuaRestartCalled = false;
[--snip--]

There is your ODR (one definition rule) violation. You have at least
two translation units where this header file got included. That makes
all of'em contain a definition of gizmo_flag_LuaRestartCalled, hence
the multiple definition error.

You get the intented behaviour by turning this definition into a
declaration and define the variable in just one translation unit (for
example, XPlaneHooks.cpp)

extern bool gizmo_flag_LuaRestartCalled; // declaration

bool gizmo_flag_LuaRestartCalled = false; // definition

Pick up a decent C++ book and learn the basics (What are translation
units? What is the concept of separate compilation? What is a
declaration? What is a definition? What is internal/external linkage?
What does the one definition rule say?)

SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top