Author Topic: 2 functions in diffrent c files that have the same name  (Read 9104 times)

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
2 functions in diffrent c files that have the same name
« on: November 03, 2013, 03:12:12 pm »
I have a sample c::b project (attached) with 4 c files, each with a main() & foo1() functions. If I go to one of these files (e.g. b.c), select management->symbols, set view = current file's symbols, select "Global functions" and then click "foo1", it will go to foo1() in another file (sometimes it's a.c::foo1 or d.c::foo1). This sounds like a bug.

Erez

Online stahta01

  • Lives here!
  • ****
  • Posts: 6996
    • My Best Post
Re: 2 functions in diffrent c files that have the same name
« Reply #1 on: November 03, 2013, 05:34:17 pm »
Please try your test on a project that DOES NOT have this error when building one of the Targets!

multiple definition of `main'

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
Re: 2 functions in diffrent c files that have the same name
« Reply #2 on: November 04, 2013, 09:45:11 am »
Please try your test on a project that DOES NOT have this error when building one of the Targets!

multiple definition of `main'

Tim S.

I will test it according to your suggestion. Still, I think that even a project that does not compile should not have this behavior, right?

Erez

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
Re: 2 functions in diffrent c files that have the same name
« Reply #3 on: November 04, 2013, 09:58:21 am »
Please try your test on a project that DOES NOT have this error when building one of the Targets!

multiple definition of `main'

Tim S.

I created my own Makefile and added it to the project (attached) as a custom Makefile. I still get the exact same behavior.

Erez

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5237
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: 2 functions in diffrent c files that have the same name
« Reply #4 on: November 05, 2013, 02:21:57 am »
I have a sample c::b project (attached) with 4 c files, each with a main() & foo1() functions. If I go to one of these files (e.g. b.c), select management->symbols, set view = current file's symbols, select "Global functions" and then click "foo1", it will go to foo1() in another file (sometimes it's a.c::foo1 or d.c::foo1). This sounds like a bug.

Erez
If the same function body is added, the later one will replace the former one. Why do we need to have two records for different functions.
If you have four header files which all have a same function declaration, and you have four source have the same function definition, how many records do you think CC need? You may expect there need 8 (this is a bit mass), but in fact, there is only ONE. CodeCompletion plugin's parser just merge all the function declaration and definition to only one record. So, you can switch from declaration and information very quickly, also keep the record database as small as it can.

BTW: CC just ignore your makefile, because it does not have the ability to parse and analysis the makefile structure and dependency rules. Anyway, if you want this feature, patches are welcome, I just don't have motion to do this. :)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
Re: 2 functions in diffrent c files that have the same name
« Reply #5 on: November 05, 2013, 08:02:54 am »
I have a sample c::b project (attached) with 4 c files, each with a main() & foo1() functions. If I go to one of these files (e.g. b.c), select management->symbols, set view = current file's symbols, select "Global functions" and then click "foo1", it will go to foo1() in another file (sometimes it's a.c::foo1 or d.c::foo1). This sounds like a bug.

Erez
If the same function body is added, the later one will replace the former one. Why do we need to have two records for different functions.
If you have four header files which all have a same function declaration, and you have four source have the same function definition, how many records do you think CC need? You may expect there need 8 (this is a bit mass), but in fact, there is only ONE. CodeCompletion plugin's parser just merge all the function declaration and definition to only one record. So, you can switch from declaration and information very quickly, also keep the record database as small as it can.

BTW: CC just ignore your makefile, because it does not have the ability to parse and analysis the makefile structure and dependency rules. Anyway, if you want this feature, patches are welcome, I just don't have motion to do this. :)


Not sure that I understand your explanation. Note that I am a c::b user, not a developer, so I don't understand the internals of CodeCompletion plugin.

I think that it's legit to have a project that its output is multiple binaries where each binary is originated from a C file, and those C files may have functions with the same name. For a user that clicks b.c::foo1() and the IDE jump to d.c::foo1(), this is very not intuitive and strange. Still, sounds like a bug to me.

Erez

ToApolytoXaos

  • Guest
Re: 2 functions in diffrent c files that have the same name
« Reply #6 on: November 05, 2013, 11:06:25 am »
erezz, please try to understand that in a C or C++ project, only one file has to use [define] main() and not in multiple sources. It's like trying to link two negative or positive sides of a magnet and wait them to merge.

If you are new to programming, I would suggest to read some good manual first and then experiment with IDEs.

What you are trying to test here will not work as it finds multiple definitions of main() function.

Always have in mind the following series of events: header file, implementation, definition [where your main() is located].

For brevity:

header file
Code: Text
  1. #ifndef header_h_
  2. #define header_h_
  3.  
  4. /* function prototype */
  5. void demo(void);
  6.  
  7. #endif
  8.  

Implementation file
Code: Text
  1. #include "header.h"
  2. #include <stdio.h>
  3.  
  4. void demo(void) {
  5.     printf("this is our demo() function.\n";
  6. }
  7.  
  8.  

Use file where main() is located.

Code: Text
  1. #include "header.h"
  2.  
  3. int main(void)
  4. {
  5.     demo();
  6.  
  7.     return 0;
  8. }
  9.  

Any constructive criticism is always welcome, but please try to understand; this is not a forum for teaching the fundamentals of programming in any language.

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
Re: 2 functions in diffrent c files that have the same name
« Reply #7 on: November 05, 2013, 12:12:19 pm »
erezz, please try to understand that in a C or C++ project, only one file has to use [define] main() and not in multiple sources. It's like trying to link two negative or positive sides of a magnet and wait them to merge.

If you are new to programming, I would suggest to read some good manual first and then experiment with IDEs.

What you are trying to test here will not work as it finds multiple definitions of main() function.

Always have in mind the following series of events: header file, implementation, definition [where your main() is located].

For brevity:

header file
Code: Text
  1. #ifndef header_h_
  2. #define header_h_
  3.  
  4. /* function prototype */
  5. void demo(void);
  6.  
  7. #endif
  8.  

Implementation file
Code: Text
  1. #include "header.h"
  2. #include <stdio.h>
  3.  
  4. void demo(void) {
  5.     printf("this is our demo() function.\n";
  6. }
  7.  
  8.  

Use file where main() is located.

Code: Text
  1. #include "header.h"
  2.  
  3. int main(void)
  4. {
  5.     demo();
  6.  
  7.     return 0;
  8. }
  9.  

Any constructive criticism is always welcome, but please try to understand; this is not a forum for teaching the fundamentals of programming in any language.

Well, I'm writing C code for ~14 years already. This thread/bug report has nothing to do with teaching the fundamentals of programming.

If you take a look at the project that I attached, you will see that the custom Makefile generates 4 binaries, hence 4 main() functions are perfectly fine.

Erez

ToApolytoXaos

  • Guest
Re: 2 functions in diffrent c files that have the same name
« Reply #8 on: November 05, 2013, 12:45:47 pm »
Well, I'm writing C code for ~14 years already. This thread/bug report has nothing to do with teaching the fundamentals of programming.

If you take a look at the project that I attached, you will see that the custom Makefile generates 4 binaries, hence 4 main() functions are perfectly fine.

Erez
Well, for someone with approximately 14 years of experience in C, I would have expected you patching it in no time and then report it to Dev team lol.

Sorry Erez, but that response of yours sounded like troll.

I rest my case.

cheers

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
Re: 2 functions in diffrent c files that have the same name
« Reply #9 on: November 05, 2013, 01:01:42 pm »
Well, I'm writing C code for ~14 years already. This thread/bug report has nothing to do with teaching the fundamentals of programming.

If you take a look at the project that I attached, you will see that the custom Makefile generates 4 binaries, hence 4 main() functions are perfectly fine.

Erez
Well, for someone with approximately 14 years of experience in C, I would have expected you patching it in no time and then report it to Dev team lol.

Sorry Erez, but that response of yours sounded like troll.

I rest my case.

cheers

1. I'm a c::b user, not a developer. I don't mind reporting c::b bugs (which I did with an example project that easily reproduces this bug), but I'm not the one to fix them.
2. Your response is irrelevant and offensive. If you have anything useful to contribute to this bug, please do. Else, I suggest that you allow other people to contribute to the conversation.

Erez

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2837
Re: 2 functions in diffrent c files that have the same name
« Reply #10 on: November 05, 2013, 02:25:29 pm »
Not sure that I understand your explanation. Note that I am a c::b user, not a developer, so I don't understand the internals of CodeCompletion plugin.

I think that it's legit to have a project that its output is multiple binaries where each binary is originated from a C file, and those C files may have functions with the same name. For a user that clicks b.c::foo1() and the IDE jump to d.c::foo1(), this is very not intuitive and strange. Still, sounds like a bug to me.

Erez

well, he said that a entry in the cc database will be overwritten from a other with the same name. This is legit, because C::B isn't made to generate multiple exe in one project (the makefile feature is simply a compatibility feature , i don't think it is thought to be used on daily use, because you won't need a IDE if you use makefiles), and  there won't be multiple entries with the same name, because it won't compile.
The right way to build multiple exe is to use the workspace feature: One workspace and multiple project files, for every exe one... Probably in the future it will be possible to use multiple workspaces at the same time..
But anyway C::B is open source so you can modify and contribute if you want.

greetings

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: 2 functions in diffrent c files that have the same name
« Reply #11 on: November 05, 2013, 02:33:45 pm »
well, he said that a entry in the cc database will be overwritten from a other with the same name. This is legit, because C::B isn't made to generate multiple exe in one project (the makefile feature is simply a compatibility feature , i don't think it is thought to be used on daily use, because you won't need a IDE if you use makefiles), and  there won't be multiple entries with the same name, because it won't compile.

This is a little overstated. What you said is normal use but its perfectly possible to use targets to generate multiple executables.

@OP: As others have said, I think this is expected behavior because CC only keeps only one copy of each function spec per project (what does that look like in the symbol browser?). It would probably be possible to support multiple function specs (as non-unique keys) but that's quite a bit of work for what is for most users a corner case.

Offline erezz

  • Multiple posting newcomer
  • *
  • Posts: 53
Re: 2 functions in diffrent c files that have the same name
« Reply #12 on: November 05, 2013, 02:41:11 pm »
Not sure that I understand your explanation. Note that I am a c::b user, not a developer, so I don't understand the internals of CodeCompletion plugin.

I think that it's legit to have a project that its output is multiple binaries where each binary is originated from a C file, and those C files may have functions with the same name. For a user that clicks b.c::foo1() and the IDE jump to d.c::foo1(), this is very not intuitive and strange. Still, sounds like a bug to me.

Erez

well, he said that a entry in the cc database will be overwritten from a other with the same name. This is legit, because C::B isn't made to generate multiple exe in one project (the makefile feature is simply a compatibility feature , i don't think it is thought to be used on daily use, because you won't need a IDE if you use makefiles), and  there won't be multiple entries with the same name, because it won't compile.
The right way to build multiple exe is to use the workspace feature: One workspace and multiple project files, for every exe one... Probably in the future it will be possible to use multiple workspaces at the same time..
But anyway C::B is open source so you can modify and contribute if you want.

greetings


Thanks for the answer. I didn't know that c::b is limited to 1 binary per project. I hope this will be fixed in the future.

I guess that many programmers have a project with one big binary and many unit tests (each unit test is a stand-alone binary), so creating a project per unit test is problematic. In my case, I will need to have ~50 projects in my workspace.

BTW - even people that use custom Makefiles need an IDE...

Thanks,
Erez

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: 2 functions in diffrent c files that have the same name
« Reply #13 on: November 05, 2013, 02:52:52 pm »

Thanks for the answer. I didn't know that c::b is limited to 1 binary per project. I hope this will be fixed in the future.

I guess that many programmers have a project with one big binary and many unit tests (each unit test is a stand-alone binary), so creating a project per unit test is problematic. In my case, I will need to have ~50 projects in my workspace.

BTW - even people that use custom Makefiles need an IDE...

Thanks,
Erez

Did you try to use targets (which are like different builds under the same project) to accomplish what you are trying to do? That would be a better option imo.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12810
    • Travis build status
Re: 2 functions in diffrent c files that have the same name
« Reply #14 on: November 05, 2013, 02:57:11 pm »
I didn't know that c::b is limited to 1 binary per project. I hope this will be fixed in the future.
It is not, it is limited to 1 binary per target and anyone telling you something else hasn't tried to open the codeblocks*.cbp projects in our repo.
You can have multiple targets per project.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]