multiple definition

Discussion in 'C++' started by Michael, Aug 23, 2011.

  1. Michael

    Michael Guest

    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
    Michael, Aug 23, 2011
    #1
    1. Advertising

  2. On 8/23/2011 12:01 PM, Michael wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 23, 2011
    #2
    1. Advertising

  3. Michael

    Michael Guest

    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
    Michael, Aug 24, 2011
    #3
  4. On 8/24/2011 2:13 AM, Michael wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 24, 2011
    #4
  5. Michael

    Michael Guest

    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
    Michael, Aug 24, 2011
    #5
  6. On 8/24/2011 12:22 PM, Michael wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 24, 2011
    #6
  7. Michael

    SG Guest

    On 24 Aug., 18:22, Michael wrote:
    > 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
    SG, Aug 24, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    585
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    415
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    445
    Jon Slaughter
    Oct 26, 2005
  4. Replies:
    11
    Views:
    1,235
    Ian Collins
    Aug 5, 2006
  5. Pierre Yves
    Replies:
    2
    Views:
    481
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page