0

Going through the RPC tutorial at MSDN, I have just created a project with two .c files as following::

/* file hellop.c */
#include <stdio.h>
#include <windows.h>

void HelloProc(char * pszString)
{
    printf("%s\n", pszString);
    return ;
}  

and

/* file: hello.c */
#include "hellop.c"

void main(void)
{
    char * pszString = "Hello, World";
    HelloProc(pszString);
    return ;
}

Problem:: Error LNK2005 and fatal Error LNK1169

Why and where is the compiler seeing the multiple symbol definition or declaration of HelloProc(char*) ?

EDIT:: As concluded in this SO_Question, including .h file is the best solution obviously. But does that leave us with no implementation of design where we can include a .c file into another .c file?

Weird Behavior:: First time compilation runs fine but rebuild of solution breaks with the above mentioned errors. You can check the multiple first time compilation by changing the file name from .c to .cpp and vice-versa. Why does it exhibit this behavior? (I am not sure if anybody else have also experienced this with the given example)

Community
  • 1
  • 1
Abhineet
  • 5,320
  • 1
  • 25
  • 43

1 Answers1

2

You compiling HelloProc twice, as you include the whole definition of of this function in hello.c file by #include "hellop.c", while you only need declaration of it. You should put function prototype in header file.

     #ifndef HELLOP_H
     #define HELLOP_H

     #include <stdio.h>
     #include <windows.h>

     void HelloProc(char * pszString);

     #endif

And include header file both in hellop.c and in hello.c

Edit: #include is not cut-paste as you said, it is more copy-paste

# include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters

so you get to linkage with two definitions of HelloProc one in hellop.c and another one in hello.c. Another way to solve it is to compile only hello.c file, this way there is no duplicate of HelloProc. See how to do it in VisualStudio here

Community
  • 1
  • 1
Dabo
  • 2,371
  • 2
  • 18
  • 26
  • Appreciate your answer but I don't think that is the issue here. Also, I don't have any "main.c" file, it is "hello.c" :-) . And yet again, I know, putting the declaration in a header file would be great but I want to know as what is the reason behind this. "#include" is just like cut-paste thing (roughly saying), then this error should not have occurred. – Abhineet Apr 02 '14 at 06:35
  • @Abhineet i checked on my machine exactly as i expected : /home/davidbo/workarea/workspace/test/Debug/../hellop.c:4: multiple definition of `HelloProc' – Dabo Apr 02 '14 at 07:10
  • Where can you see the two definitions of HelloProc? And as you have stated, #include would work like copy-paste, so the whole "hellop.c" will be replaced in hello.c and would be compiled. And in any case, I have already tried guards (they are the first ones to try whenever I am hit with LNK2005), but doesn't seems to work. – Abhineet Apr 02 '14 at 07:11
  • So, does the guards help? :-) – Abhineet Apr 02 '14 at 07:12
  • so include is copy-paste, meaning at linkage you have two definitions of `HelloProc` one in hellop.obj here it compiled for the first time, and in hello.obj here you get another definition of same function. So it compiled twice, and now it is trying to link `HelloProc` twice. – Dabo Apr 02 '14 at 07:23
  • i found this [this](http://stackoverflow.com/a/672785/2549281) answer on similar question with an explanation much better then mine :) . Looks like you have no choice but to add header file. – Dabo Apr 02 '14 at 07:24
  • So, it means, there is no way to include .c files into another .c file? – Abhineet Apr 02 '14 at 07:33
  • @Abhineet There is, but then you have to build only `hello.c` file – Dabo Apr 02 '14 at 07:39
  • And, how would I do that? Also, do you experienced that, the given example builds perfectly the first time it is compiled and then breaks on "rebuild" ??? – Abhineet Apr 02 '14 at 07:44
  • +1 for your solution. But I am not accepting it for now because I still have some doubts. :-) thanks for the long discussion and your patience to keep up with my never-ending doubts :-) – Abhineet Apr 02 '14 at 07:47
  • I'm on linux with gcc compiler, i got the error message directly at first build. I guess there some Microsoft workarounds so you get errors only at rebuild – Dabo Apr 02 '14 at 07:49