PROS C++ porting issues

I was trying to see if it were possible to use C++ in my pros project instead of C so that I use classes to organize the mess of variables and functions that is my code. Through pros doesn’t seem to reject the classes or the .cpp files, I come across a multiple definition error from the linker. This even happens if the .cpp specific content is removed; if the src files are .cpp I get the errors, but not when they are .c files it works fine. I have proper header guards. The only things that get multiple definitions are objects for encoders and the gyro, though any other objects that I add also have the same issues. If anyone has any ideas how to fix it that would be greatly appreciated.

Do you have #ifndef’s in your header files you’re including?

Yes that’s what I mean by “proper header guards”.

Can you post the errors.

Scratch what I said about it only being objects. All global variables spur errors. It goes on like that for a while. However it is fine with .c files with identical code.
15217714804201968461246352813424.jpg
15217716461948041108616165359576.jpg
1521771799399627063453330635023.jpg

It seems to imply you have the same function in more than one source file. For example, “shifterCheck”, where is that function defined ? autons.c or autoAPI.c ?

“shifterCheck” is a Boolean to track whether the 4-bar motors and sensors are properly working. None of the errors are from functions, only variables and objects. One of the pics shows the files compile fine with .c extensions. But it gets confused with the .cpp extensions, which are necessary for using classes. Also it seems to be an issue with the main.h because it shows errors for every global variable in each file which includes main.h.

Upload your project. Preferably the simplest version that still has this problem.

Are you declaring any C variables and functions you use as extern “C” ?
Typically if you include a header in a C++ file that has prototypes for functions in a C file you need to add

#ifdef __cplusplus
extern "C" {
#endif

// prototypes etc.

#ifdef __cplusplus
}
#endif

Yeah I’m fairly certain I have that.

Like jpearman said, make sure to use extern C.

Here is an example of a tested PROS program written entirely in C++ and using classes: https://github.com/niwhsa9/VEX_2017-2018_Team2496V_CODE/tree/V3

This article may help: https://blog.pxtst.com/using-c-in-pros/

You’ll have some issues if you are using pure virtual too. You can see how to fix that in the example project I linked you in src/subsystem.cpp

Here is a zip file of the project. Sorry if that is inconvenient, it’s the only way I could get it to upload.
worlds-3.22.18.zip (3.2 MB)

Ok, couple of problems.
you define all your functions as extern “C”, but they are not if they are in C++ files.
you include this in every source.

#ifndef _STATUS_H_
#define _STATUS_H_

int statusCode;
bool voltageCheck_Main, voltageCheck_Expander, voltageCheck_Backup; // battery charged y/n
// status of each mechanism: 0 is working, 1 is encoder issue, 2 is motor issue.
int liftCheck, driveCheck, mogoCheck, shifterCheck;
char problems[10];

void systemCheck();

#endif // _STATUS_H_

So you have multiple definitions of all of those variables. The include guards only stop you including that file more than once in a single source, not across several source files.

A global variable should only be declared once in a program


int statusCode;

everywhere else you want to use that declare it as extern.


extern int statusCode;

Here’s a hacked version that builds, not really how it should be done but you can fix it up properly from here.
worlds-3.22.18_rev1.zip (3.26 MB)

Ok thanks I’ll see what I can do.