Page 1 of 1

NBEclipse does not compile .c files

Posted: Thu Nov 05, 2020 5:53 am
by Anguel
Hi,

I would like to share some code in a .c file between Netburner and STM32.
Unfortunately, NBEclipse does not seem to compile those .c files, if I rename them to .cpp they compile fine.
Is there a way to fix this?

Thanks,
Anguel

Re: NBEclipse does not compile .c files

Posted: Thu Nov 05, 2020 6:23 am
by TomNB
Hello Anguel,

This is a C vs C++ issue, not an eclipse issue. Any time you want to use functions in a .cpp file that are located in a .c file, you must reference it as extern "C". It is called name mangling: https://www.tutorialspoint.com/name-man ... -cplusplus. Does the compiler you are using for the STM32 support C++? If so, making them both .cpp would be easiest.

Re: NBEclipse does not compile .c files

Posted: Thu Nov 05, 2020 7:25 am
by Anguel
Hi Tom,

Thanks for the help. I actually tried to add the extern "C" but maybe I did something wrong as it did not work.
Now I renamed to .cpp and the STM32 compiles fine.

Thanks,
Anguel

Re: NBEclipse does not compile .c files

Posted: Thu Nov 05, 2020 7:56 am
by TomNB
Hi,

Just in case it comes up in the future, here is an example. Normally you would use header files, but just trying to make this simple.

Lets say you have a file foo.c with a function funcFoo():

Code: Select all

void funcFoo()
{
	iprintf("Executing funcFoo\r\n");
}
In your main.cpp you then try to call it:

Code: Select all

extern void funcFoo();

void UserMain(void * pd) {
    InitializeStack();
    GetDHCPAddressIfNecessary();
    OSChangePrio(MAIN_PRIO);
    EnableAutoUpdate();
    StartHTTP();
    
    iprintf("Application started\n");

    funcFoo();

    while (1) {
        OSTimeDly(TICKS_PER_SECOND);
    }
}
But the error you get is:
C:\nburn\workspace\AppWiz\Release/..\main.cpp:36: undefined reference to `funcFoo()'

This is because with C++ name mangling the signature of funcFoo doesn't match what is in foo.c. So lets change the extern in main.cpp to:

Code: Select all

extern "C" {
	void funcFoo();
}
Now it builds fine. The bracket format is used so you can add as many C functions as you want between the brackets, but if its just one you could make it extern "C" funcFoo();

Re: NBEclipse does not compile .c files

Posted: Thu Nov 05, 2020 8:22 am
by Anguel
UPDATE:
It turned out just changing the file to .cpp did NOT work as the STM32CubeIDE (Eclipse based) project is set to C, not to C++ right now, so it just did not compile the .cpp at all but used a version of the old .c I had previously compiled.
That confused me as it built ok until I changed the file.

The problem that my previous extern "C" experiments did not work was actually that I had not included the #ifdef __cplusplus part and this confused the C-only compiler of the STM32:
#ifdef __cplusplus
extern "C" {
#endif

See here: https://isocpp.org/wiki/faq/mixing-c-an ... s-personal

So this is very important to make the C-only compiler skip the extern "C" it does not understand. This is at least how I understand things so far.
It finally seeams to work fine now: with a .c file and the #ifdef __cplusplus part.

Anguel

Re: NBEclipse does not compile .c files

Posted: Fri Dec 18, 2020 4:56 am
by Anguel
Now my problems continue. I wanted to use the Get_msec() function into my .c file shared between Netburner and STM32. It is conditionally compiled on the Netburner platform only, together with a conditionally added #include <utils.h>

The problem is that when I add these, suddenly hundreds of errors appear:
Errors_when_including_utils.h_for_Netburner.png
Errors_when_including_utils.h_for_Netburner.png (31.47 KiB) Viewed 17968 times
I think this may have something to do with the
#ifdef __cplusplus
extern "C" {
#endif
added in the corresponding .h file.
Any idea how to solve this?

Re: NBEclipse does not compile .c files

Posted: Fri Dec 18, 2020 9:40 am
by TomNB
Hello,

I don't think there is anything NetBurner specific here, but in general its mixing C and C++ code with the NNDK using gcc. Eclipse does not compile anything, it is just an editor and project manager. I am not familiar with the STM32CubeIDE, or what compiler it uses to build those projects. So any information you can find on mixing C and C++ should be applicable. Doing a quick search I found this:
https://www.oracle.com/technical-resour ... splus.html

I have not tried to call a C++ function from a C source file, but it sounds like there are a lot of issues to consider. Also this:

https://www.quora.com/How-do-I-call-a-C ... ion-from-C
"About calling C++ functions and class member functions ‘as is’ from C, there is no easy and obvious way to do it. What you can do is, expose a C API of your codes written in C++ and use that API in C. You can go through the book mentioned above and explore ideas about creating elaborate C APIs for object oriented code written in C++."

I hope these references are of some help.

Re: NBEclipse does not compile .c files

Posted: Fri Dec 18, 2020 11:30 am
by Anguel
When I add the "extern "C"" but then have to #include a header from Nbrtos something does not work out well and all the errors are thrown. Maybe it is a configuration error, maybe it is because these headers expect C++, not C.

Actually I am not mixing C and C++, I just want a .c file to be compiled as if it were a .cpp by the the NB Eclipse. The problem is that NB invokes GCC instead of G++ when it sees a .c file. I don't want that to happen, I want the C++ compiler to process it as if it were a .cpp file.

Meanwhile I found out that there is a "-x c++" switch that can be passed to the compiler to do what I want.
But I am not sure if it is still supported by the latest gcc version and where exactly I should add it into the Eclipse NB config.
I am sure your expert who configures the toolchain has an idea how to do it properly. Thanks in advance.

Anguel

Re: NBEclipse does not compile .c files

Posted: Fri Dec 18, 2020 12:54 pm
by TomNB
I will check on that. But in your previous post you made the statement below. Wouldn't that be the same when forcing G++?

Post by Anguel » Thu Nov 05, 2020 9:22 am
UPDATE:
It turned out just changing the file to .cpp did NOT work as the STM32CubeIDE (Eclipse based) project is set to C, not to C++ right now, so it just did not compile the .cpp at all but used a version of the old .c I had previously compiled.
That confused me as it built ok until I changed the file.

Re: NBEclipse does not compile .c files

Posted: Fri Dec 18, 2020 1:05 pm
by Anguel
Finally I managed to compile successfully with a workaround. STM32CubeIDE is configured C only and just ignores all .cpp files. So I did a simple
#include "file.cpp"
in a file.c and now STM32 compiles the code as C while Netburner compiles it as C++, of course file.c has to be in a folder NB does not know about, otherwise it also tries to compile it in addition.
Such a nightmare and so many hours spent to just share a single file with code between a C and a C++ project. I would be still happy if someone can tell me what caused all those errors when I work with "extern "C"" and want to use a Nbrtos function at the same time.