Multiple definition of function in the same place





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















I am trying to simulate generics in C by having some preprocessor definitions for a matrix type. Here is an excerpt of that:



#define __matrix_struct(TYPE) 
struct {
uint32_t sz;
TYPE **ptr;
}

#define __matrix_t(TYPE) matrix_ ## TYPE
#define __matrix_ptr_t(TYPE) __matrix_t(TYPE) *

#define __matrix_typedef(TYPE) typedef __matrix_struct(TYPE) __matrix_t(TYPE)

#define __matrix_allocator_name(TYPE) TYPE ## _matrix_alloc
#define __matrix_allocator(TYPE)
__matrix_ptr_t(TYPE) __matrix_allocator_name(TYPE) (uint32_t sz) {
uint32_t i;
__matrix_ptr_t(TYPE) m = (__matrix_ptr_t(TYPE)) malloc(sizeof(__matrix_t(TYPE)));
m->ptr = (TYPE **) malloc(sz * sizeof(TYPE *));
for (i = 0; i < sz; ++i) {
m->ptr[i] = (TYPE *) calloc(sz, sizeof(TYPE));
}
return m;
}

#define __matrix_deallocator_name(TYPE) TYPE ## _matrix_free
#define __matrix_deallocator(TYPE)
void __matrix_deallocator_name(TYPE) (__matrix_ptr_t(TYPE) m) {
uint32_t i;
for (i = 0; i < m->sz; i++) {
free(m->ptr[i]);
}
free(m->ptr);
free(m);
}

#define matrix_alloc_ptr(TYPE, SIZE) __matrix_allocator_name(TYPE) (SIZE)
#define matrix_dealloc_ptr(TYPE, PTR_NAME) __matrix_deallocator_name(TYPE) (PTR_NAME)


In another file, byte_matrix.h, I am trying to define a matrix of uint8_t values, as follows:



#include "matrix.h"

typedef uint8_t byte;

__matrix_typedef(byte);

__matrix_allocator(byte)
__matrix_deallocator(byte)


When I try to compile, I get the following errors:



CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_alloc':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: multiple definition of `byte_matrix_alloc'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: first defined here
CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_free':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: multiple definition of `byte_matrix_free'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: first defined here


I cannot understand why it would point to times to the same line and complain about that definition, since every header I wrote has include guards. Could you please explain this to me? Also if you know of a better approach to my problem, please let me know. Thanks.



Also I need to compile with -std=c99 if that matters in this case.










share|improve this question




















  • 1





    Did you try to run only the preprocessor?

    – Amessihel
    Nov 16 '18 at 10:31






  • 2





    You are including byte_matrix.h in multiple compilation units, which means you get multiple function definitions. You need to make these functions static.

    – Groo
    Nov 16 '18 at 10:35








  • 1





    You can use two macros -- __matrix_allocator_declare for h-files and __matrix_allocator_define for one (selected by you) c-file.

    – ReAl
    Nov 16 '18 at 10:36






  • 2





    If you don't use static, functions are by default extern. This means that you will have multiple functions named byte_matrix_alloc in your program. If you use static, then you are saying that this function is private to each compilation unit, i.e. each .c file gets it's own copy of the function.

    – Groo
    Nov 16 '18 at 10:38






  • 2





    Interesting, C11 (C99 successor) provides type-generic expressions.

    – Amessihel
    Nov 16 '18 at 10:45




















6















I am trying to simulate generics in C by having some preprocessor definitions for a matrix type. Here is an excerpt of that:



#define __matrix_struct(TYPE) 
struct {
uint32_t sz;
TYPE **ptr;
}

#define __matrix_t(TYPE) matrix_ ## TYPE
#define __matrix_ptr_t(TYPE) __matrix_t(TYPE) *

#define __matrix_typedef(TYPE) typedef __matrix_struct(TYPE) __matrix_t(TYPE)

#define __matrix_allocator_name(TYPE) TYPE ## _matrix_alloc
#define __matrix_allocator(TYPE)
__matrix_ptr_t(TYPE) __matrix_allocator_name(TYPE) (uint32_t sz) {
uint32_t i;
__matrix_ptr_t(TYPE) m = (__matrix_ptr_t(TYPE)) malloc(sizeof(__matrix_t(TYPE)));
m->ptr = (TYPE **) malloc(sz * sizeof(TYPE *));
for (i = 0; i < sz; ++i) {
m->ptr[i] = (TYPE *) calloc(sz, sizeof(TYPE));
}
return m;
}

#define __matrix_deallocator_name(TYPE) TYPE ## _matrix_free
#define __matrix_deallocator(TYPE)
void __matrix_deallocator_name(TYPE) (__matrix_ptr_t(TYPE) m) {
uint32_t i;
for (i = 0; i < m->sz; i++) {
free(m->ptr[i]);
}
free(m->ptr);
free(m);
}

#define matrix_alloc_ptr(TYPE, SIZE) __matrix_allocator_name(TYPE) (SIZE)
#define matrix_dealloc_ptr(TYPE, PTR_NAME) __matrix_deallocator_name(TYPE) (PTR_NAME)


In another file, byte_matrix.h, I am trying to define a matrix of uint8_t values, as follows:



#include "matrix.h"

typedef uint8_t byte;

__matrix_typedef(byte);

__matrix_allocator(byte)
__matrix_deallocator(byte)


When I try to compile, I get the following errors:



CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_alloc':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: multiple definition of `byte_matrix_alloc'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: first defined here
CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_free':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: multiple definition of `byte_matrix_free'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: first defined here


I cannot understand why it would point to times to the same line and complain about that definition, since every header I wrote has include guards. Could you please explain this to me? Also if you know of a better approach to my problem, please let me know. Thanks.



Also I need to compile with -std=c99 if that matters in this case.










share|improve this question




















  • 1





    Did you try to run only the preprocessor?

    – Amessihel
    Nov 16 '18 at 10:31






  • 2





    You are including byte_matrix.h in multiple compilation units, which means you get multiple function definitions. You need to make these functions static.

    – Groo
    Nov 16 '18 at 10:35








  • 1





    You can use two macros -- __matrix_allocator_declare for h-files and __matrix_allocator_define for one (selected by you) c-file.

    – ReAl
    Nov 16 '18 at 10:36






  • 2





    If you don't use static, functions are by default extern. This means that you will have multiple functions named byte_matrix_alloc in your program. If you use static, then you are saying that this function is private to each compilation unit, i.e. each .c file gets it's own copy of the function.

    – Groo
    Nov 16 '18 at 10:38






  • 2





    Interesting, C11 (C99 successor) provides type-generic expressions.

    – Amessihel
    Nov 16 '18 at 10:45
















6












6








6








I am trying to simulate generics in C by having some preprocessor definitions for a matrix type. Here is an excerpt of that:



#define __matrix_struct(TYPE) 
struct {
uint32_t sz;
TYPE **ptr;
}

#define __matrix_t(TYPE) matrix_ ## TYPE
#define __matrix_ptr_t(TYPE) __matrix_t(TYPE) *

#define __matrix_typedef(TYPE) typedef __matrix_struct(TYPE) __matrix_t(TYPE)

#define __matrix_allocator_name(TYPE) TYPE ## _matrix_alloc
#define __matrix_allocator(TYPE)
__matrix_ptr_t(TYPE) __matrix_allocator_name(TYPE) (uint32_t sz) {
uint32_t i;
__matrix_ptr_t(TYPE) m = (__matrix_ptr_t(TYPE)) malloc(sizeof(__matrix_t(TYPE)));
m->ptr = (TYPE **) malloc(sz * sizeof(TYPE *));
for (i = 0; i < sz; ++i) {
m->ptr[i] = (TYPE *) calloc(sz, sizeof(TYPE));
}
return m;
}

#define __matrix_deallocator_name(TYPE) TYPE ## _matrix_free
#define __matrix_deallocator(TYPE)
void __matrix_deallocator_name(TYPE) (__matrix_ptr_t(TYPE) m) {
uint32_t i;
for (i = 0; i < m->sz; i++) {
free(m->ptr[i]);
}
free(m->ptr);
free(m);
}

#define matrix_alloc_ptr(TYPE, SIZE) __matrix_allocator_name(TYPE) (SIZE)
#define matrix_dealloc_ptr(TYPE, PTR_NAME) __matrix_deallocator_name(TYPE) (PTR_NAME)


In another file, byte_matrix.h, I am trying to define a matrix of uint8_t values, as follows:



#include "matrix.h"

typedef uint8_t byte;

__matrix_typedef(byte);

__matrix_allocator(byte)
__matrix_deallocator(byte)


When I try to compile, I get the following errors:



CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_alloc':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: multiple definition of `byte_matrix_alloc'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: first defined here
CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_free':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: multiple definition of `byte_matrix_free'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: first defined here


I cannot understand why it would point to times to the same line and complain about that definition, since every header I wrote has include guards. Could you please explain this to me? Also if you know of a better approach to my problem, please let me know. Thanks.



Also I need to compile with -std=c99 if that matters in this case.










share|improve this question
















I am trying to simulate generics in C by having some preprocessor definitions for a matrix type. Here is an excerpt of that:



#define __matrix_struct(TYPE) 
struct {
uint32_t sz;
TYPE **ptr;
}

#define __matrix_t(TYPE) matrix_ ## TYPE
#define __matrix_ptr_t(TYPE) __matrix_t(TYPE) *

#define __matrix_typedef(TYPE) typedef __matrix_struct(TYPE) __matrix_t(TYPE)

#define __matrix_allocator_name(TYPE) TYPE ## _matrix_alloc
#define __matrix_allocator(TYPE)
__matrix_ptr_t(TYPE) __matrix_allocator_name(TYPE) (uint32_t sz) {
uint32_t i;
__matrix_ptr_t(TYPE) m = (__matrix_ptr_t(TYPE)) malloc(sizeof(__matrix_t(TYPE)));
m->ptr = (TYPE **) malloc(sz * sizeof(TYPE *));
for (i = 0; i < sz; ++i) {
m->ptr[i] = (TYPE *) calloc(sz, sizeof(TYPE));
}
return m;
}

#define __matrix_deallocator_name(TYPE) TYPE ## _matrix_free
#define __matrix_deallocator(TYPE)
void __matrix_deallocator_name(TYPE) (__matrix_ptr_t(TYPE) m) {
uint32_t i;
for (i = 0; i < m->sz; i++) {
free(m->ptr[i]);
}
free(m->ptr);
free(m);
}

#define matrix_alloc_ptr(TYPE, SIZE) __matrix_allocator_name(TYPE) (SIZE)
#define matrix_dealloc_ptr(TYPE, PTR_NAME) __matrix_deallocator_name(TYPE) (PTR_NAME)


In another file, byte_matrix.h, I am trying to define a matrix of uint8_t values, as follows:



#include "matrix.h"

typedef uint8_t byte;

__matrix_typedef(byte);

__matrix_allocator(byte)
__matrix_deallocator(byte)


When I try to compile, I get the following errors:



CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_alloc':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: multiple definition of `byte_matrix_alloc'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:13: first defined here
CMakeFiles/tictac.dir/game/board.c.o: In function `byte_matrix_free':
/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: multiple definition of `byte_matrix_free'
CMakeFiles/tictac.dir/main.c.o:/home/victor/dev/pc/tictac/game/../matrix/byte_matrix.h:14: first defined here


I cannot understand why it would point to times to the same line and complain about that definition, since every header I wrote has include guards. Could you please explain this to me? Also if you know of a better approach to my problem, please let me know. Thanks.



Also I need to compile with -std=c99 if that matters in this case.







c c99 generic-programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:07









Amessihel

2,6991826




2,6991826










asked Nov 16 '18 at 10:24









VictorVictor

5,5941246105




5,5941246105








  • 1





    Did you try to run only the preprocessor?

    – Amessihel
    Nov 16 '18 at 10:31






  • 2





    You are including byte_matrix.h in multiple compilation units, which means you get multiple function definitions. You need to make these functions static.

    – Groo
    Nov 16 '18 at 10:35








  • 1





    You can use two macros -- __matrix_allocator_declare for h-files and __matrix_allocator_define for one (selected by you) c-file.

    – ReAl
    Nov 16 '18 at 10:36






  • 2





    If you don't use static, functions are by default extern. This means that you will have multiple functions named byte_matrix_alloc in your program. If you use static, then you are saying that this function is private to each compilation unit, i.e. each .c file gets it's own copy of the function.

    – Groo
    Nov 16 '18 at 10:38






  • 2





    Interesting, C11 (C99 successor) provides type-generic expressions.

    – Amessihel
    Nov 16 '18 at 10:45
















  • 1





    Did you try to run only the preprocessor?

    – Amessihel
    Nov 16 '18 at 10:31






  • 2





    You are including byte_matrix.h in multiple compilation units, which means you get multiple function definitions. You need to make these functions static.

    – Groo
    Nov 16 '18 at 10:35








  • 1





    You can use two macros -- __matrix_allocator_declare for h-files and __matrix_allocator_define for one (selected by you) c-file.

    – ReAl
    Nov 16 '18 at 10:36






  • 2





    If you don't use static, functions are by default extern. This means that you will have multiple functions named byte_matrix_alloc in your program. If you use static, then you are saying that this function is private to each compilation unit, i.e. each .c file gets it's own copy of the function.

    – Groo
    Nov 16 '18 at 10:38






  • 2





    Interesting, C11 (C99 successor) provides type-generic expressions.

    – Amessihel
    Nov 16 '18 at 10:45










1




1





Did you try to run only the preprocessor?

– Amessihel
Nov 16 '18 at 10:31





Did you try to run only the preprocessor?

– Amessihel
Nov 16 '18 at 10:31




2




2





You are including byte_matrix.h in multiple compilation units, which means you get multiple function definitions. You need to make these functions static.

– Groo
Nov 16 '18 at 10:35







You are including byte_matrix.h in multiple compilation units, which means you get multiple function definitions. You need to make these functions static.

– Groo
Nov 16 '18 at 10:35






1




1





You can use two macros -- __matrix_allocator_declare for h-files and __matrix_allocator_define for one (selected by you) c-file.

– ReAl
Nov 16 '18 at 10:36





You can use two macros -- __matrix_allocator_declare for h-files and __matrix_allocator_define for one (selected by you) c-file.

– ReAl
Nov 16 '18 at 10:36




2




2





If you don't use static, functions are by default extern. This means that you will have multiple functions named byte_matrix_alloc in your program. If you use static, then you are saying that this function is private to each compilation unit, i.e. each .c file gets it's own copy of the function.

– Groo
Nov 16 '18 at 10:38





If you don't use static, functions are by default extern. This means that you will have multiple functions named byte_matrix_alloc in your program. If you use static, then you are saying that this function is private to each compilation unit, i.e. each .c file gets it's own copy of the function.

– Groo
Nov 16 '18 at 10:38




2




2





Interesting, C11 (C99 successor) provides type-generic expressions.

– Amessihel
Nov 16 '18 at 10:45







Interesting, C11 (C99 successor) provides type-generic expressions.

– Amessihel
Nov 16 '18 at 10:45














2 Answers
2






active

oldest

votes


















3














A quick fix would be to add static to your function definitions. This will create a static copy of these functions in each compilation unit which references the header. If you want the functions to be inlined every time, this is the way to go.



An alternative way to do it would be to keep function declarations in a .h file, and actual definitions in a single .c file. This approach will avoid duplication, and the compiler will not inline them (unless your linker supports link time optimization).



The reason is that you are including this header file in multiple compilation units. After the preprocessor does all the textual replacements, you end up with actual separate function definitions inside your .c files. And if you don't specify that you want them to be static, they are by default extern, which means that now the compiler doesn't know how to differentiate them if some other part of the code wants to call them.



This is what you basically do whenever you create a header file: you create a list of declarations which will be included in many compilation units, but there is always a single extern definition in a single .c file.






share|improve this answer































    2














    Another way (relative to the proposed by Groo) is to create two macros.





    • __matrix_allocator_declare with just prototype of function -- for h-file(s)


    • __matrix_allocator_define with function body -- for one (selected by you) c-file


    This way requires to handle two macros and to not forget add function-body macro in some file, but (and it is more important for embedded applications on small microcontrollers) it guarantees that only one function instance will consume memory.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335887%2fmultiple-definition-of-function-in-the-same-place%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      A quick fix would be to add static to your function definitions. This will create a static copy of these functions in each compilation unit which references the header. If you want the functions to be inlined every time, this is the way to go.



      An alternative way to do it would be to keep function declarations in a .h file, and actual definitions in a single .c file. This approach will avoid duplication, and the compiler will not inline them (unless your linker supports link time optimization).



      The reason is that you are including this header file in multiple compilation units. After the preprocessor does all the textual replacements, you end up with actual separate function definitions inside your .c files. And if you don't specify that you want them to be static, they are by default extern, which means that now the compiler doesn't know how to differentiate them if some other part of the code wants to call them.



      This is what you basically do whenever you create a header file: you create a list of declarations which will be included in many compilation units, but there is always a single extern definition in a single .c file.






      share|improve this answer




























        3














        A quick fix would be to add static to your function definitions. This will create a static copy of these functions in each compilation unit which references the header. If you want the functions to be inlined every time, this is the way to go.



        An alternative way to do it would be to keep function declarations in a .h file, and actual definitions in a single .c file. This approach will avoid duplication, and the compiler will not inline them (unless your linker supports link time optimization).



        The reason is that you are including this header file in multiple compilation units. After the preprocessor does all the textual replacements, you end up with actual separate function definitions inside your .c files. And if you don't specify that you want them to be static, they are by default extern, which means that now the compiler doesn't know how to differentiate them if some other part of the code wants to call them.



        This is what you basically do whenever you create a header file: you create a list of declarations which will be included in many compilation units, but there is always a single extern definition in a single .c file.






        share|improve this answer


























          3












          3








          3







          A quick fix would be to add static to your function definitions. This will create a static copy of these functions in each compilation unit which references the header. If you want the functions to be inlined every time, this is the way to go.



          An alternative way to do it would be to keep function declarations in a .h file, and actual definitions in a single .c file. This approach will avoid duplication, and the compiler will not inline them (unless your linker supports link time optimization).



          The reason is that you are including this header file in multiple compilation units. After the preprocessor does all the textual replacements, you end up with actual separate function definitions inside your .c files. And if you don't specify that you want them to be static, they are by default extern, which means that now the compiler doesn't know how to differentiate them if some other part of the code wants to call them.



          This is what you basically do whenever you create a header file: you create a list of declarations which will be included in many compilation units, but there is always a single extern definition in a single .c file.






          share|improve this answer













          A quick fix would be to add static to your function definitions. This will create a static copy of these functions in each compilation unit which references the header. If you want the functions to be inlined every time, this is the way to go.



          An alternative way to do it would be to keep function declarations in a .h file, and actual definitions in a single .c file. This approach will avoid duplication, and the compiler will not inline them (unless your linker supports link time optimization).



          The reason is that you are including this header file in multiple compilation units. After the preprocessor does all the textual replacements, you end up with actual separate function definitions inside your .c files. And if you don't specify that you want them to be static, they are by default extern, which means that now the compiler doesn't know how to differentiate them if some other part of the code wants to call them.



          This is what you basically do whenever you create a header file: you create a list of declarations which will be included in many compilation units, but there is always a single extern definition in a single .c file.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 10:42









          GrooGroo

          36k1490160




          36k1490160

























              2














              Another way (relative to the proposed by Groo) is to create two macros.





              • __matrix_allocator_declare with just prototype of function -- for h-file(s)


              • __matrix_allocator_define with function body -- for one (selected by you) c-file


              This way requires to handle two macros and to not forget add function-body macro in some file, but (and it is more important for embedded applications on small microcontrollers) it guarantees that only one function instance will consume memory.






              share|improve this answer




























                2














                Another way (relative to the proposed by Groo) is to create two macros.





                • __matrix_allocator_declare with just prototype of function -- for h-file(s)


                • __matrix_allocator_define with function body -- for one (selected by you) c-file


                This way requires to handle two macros and to not forget add function-body macro in some file, but (and it is more important for embedded applications on small microcontrollers) it guarantees that only one function instance will consume memory.






                share|improve this answer


























                  2












                  2








                  2







                  Another way (relative to the proposed by Groo) is to create two macros.





                  • __matrix_allocator_declare with just prototype of function -- for h-file(s)


                  • __matrix_allocator_define with function body -- for one (selected by you) c-file


                  This way requires to handle two macros and to not forget add function-body macro in some file, but (and it is more important for embedded applications on small microcontrollers) it guarantees that only one function instance will consume memory.






                  share|improve this answer













                  Another way (relative to the proposed by Groo) is to create two macros.





                  • __matrix_allocator_declare with just prototype of function -- for h-file(s)


                  • __matrix_allocator_define with function body -- for one (selected by you) c-file


                  This way requires to handle two macros and to not forget add function-body macro in some file, but (and it is more important for embedded applications on small microcontrollers) it guarantees that only one function instance will consume memory.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 10:58









                  ReAlReAl

                  8001317




                  8001317






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335887%2fmultiple-definition-of-function-in-the-same-place%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values