Problems with const float variables

I have been having a problem with const float variables. This is a simplified version of the code in one of my header files:

namespace Arm
{
  const float maxRotation = 2.7;
  const float kP = 8;
  const float kT = 1.5;
  
  // Other variables/functions here
}

The problem is that whenever I change one of the const floats, it doesn’t actually change the program. If I change the kP value, for instance, and then download the program and have it display the kP value to the LCD screen, it shows the old value. However, if I keep downloading the program, eventually it sometimes works. The wierdest part is, if I delete the declaration of one of those variables altogether, the program still compiles fine, even though it is using a variable that no longer exists. The VEXcode editor highlights the usage of that variable as an error, but it still downloads fine. It’s almost as if const variables are not recompiled every time you build the program.

Also, not sure if this is a related problem, but sometimes when I try to upload the code it just says “Error!” and does not upload. I have to keep uploading the code, and eventually it works. When this happens, this is what it outputs:

[info]: Saving Project ...
[info]: Project saved!
windows build for platform vexv5
make: Nothing to be done for 'all'.
[info]: build completed!
[info]: download
[error]: download error!

Lol, this has nothing to do with const float variables, it is how the build system works. When you build your project, the compiler takes each of the source (cpp) files, copy/pastes all included header files into it, and then compiles it.

However, the build system (make) uses an optimization, where it will not recompile source files if the file was not modified since last compilation. When you have multiple source files, this speeds up the build process, as it only needs to recompile modified files.

Since header files are not directly compiled (they are copied to the source file), changing them won’t be recorded as a change to your source file.

Therefore, even though you changed your header, the compiler sees that the source files were not modified, so it skips compiling. This is exactly what you see when it outputs make: Nothing to be done for 'all'. So, it just uploads the last compiled binary.

The way to make it rebuild the source file is simply to edit the source file.

This problem happened because of bad programming practice. You are not meant to define objects in header files, only declare (forward declare) them. You should have your actual values somewhere in a source file.

You should read up on some C++ tutorials on how header files work and how to manage a multiple-file project.

5 Likes

Thanks for the reply. For all of the non-constant variables in my program, I already do declare them in header files and define them in source files. I didn’t realize that you also need to do this for constants too! I’m used to Java, so I’m still getting used to the system of header and source files in c++ :smile:

Most of the make packages come with some sort of makedepend tool that could analyse source files and add header dependency information to the makefile. Then, if any of the headers listed in the makefile change, that will trigger recompilation of all relevant source files.

I don’t know if such make depend command is already included in the build process or could be easily integrated into VEXcode workflow. It would be very useful though.

1 Like

Yeah, PROS implemented header dependency tracking in the latest release, its definitely doable. Would be a good feature for VEXcode.

1 Like

Kinda off topic, but try to use doubles instead of floats. This is because floats use less bits when storing decimals as opposed to doubles. This doesn’t apply in higher level languages like Python, however.

VEXcode tracks header files in the include folder only. Other locations can be added to the makefile as necessary. you would update these two lines

# location of include files that c and cpp files depend on
SRC_H  = $(wildcard include/*.h)

# project header file locations
INC_F  = include
5 Likes