Help with using multiple files in VEX Studio

Hello, I am looking for help on how to use multiple source files for my VEX IQ project. I want to extract some functions related to the drivetrain into their own file, like I would with one of my hobby C++ projects. However, I am getting multiple definition errors like the following:

build/src/Maneuvers.o:(.bss.BlockMotor+0x0): multiple definition of `BlockMotor’
build/src/main.o:(.bss.BlockMotor+0x0): first defined here

If I have the header file that is defining the function included in two different files. I have tried using both the “pragma once” macro and the “ifndef MYHEADER_H…” macros that normally prevent header files from being included twice, but neither of them seem to prevent the error.

Is there any special VEX macro that can be used or something? I am wondering of the compiler used on my code is a more outdated g++ compiler or one without the feature to prevent double-header inclusions.

What are you using to program the IQ with ?

1 Like

I am using VEXCode V5 Text, and programming a VEX V5 core.

make sure you include extern (device) of all of the devices you use outside of the file you declared in. You’ll only need to declare it once, your error is indicating that you defined it in main and also in ‘maneuvers’

Also rn this thread’s category is for IQ, this is an EDR discussion.

1 Like

I’ve tried using the extern, but it seems to give the same error. I understand that the error indicates that I am defining the devices multiple times, but I have only defined them once in their own separate header file. The issue only seems to come up when add more header/cpp files that main includes. If I put all the code from the cpp files into the header files and set up the includes in a way where it no headers are included in main twice, then it compiles.

The error seems to come up whenever I include the line

#include “Driving.h”

or if I include the shared header file directly, inside of my Driving.cpp file. If that line is not there, then the error does not show up.

How are you using extern then? It should just be extern devicetype devicename; without any params. I’m having trouble understanding exactly what’s happening without the code, if it isn’t a problem with you can you export your project so we can see all of the files?

1 Like

I believe I am using extern the right way. I totally understand! Here is a link to my files, since new users cannot upload files:

The file that these devices are defined in is PortConfig.h.

Ignore this post, see the next one

previous contents

There’s a bit here so just bear with me


EDIT: I need to update vexcode, I don’t usually use this pc for coding, it’s running on version 0.9 or something… so there absolutely are flaws in the comments below for the time being

I’m not sure where setTurnVelocity and setDriveVelocity is declared so I can’t really help there.

stating functions as in driving.h should be in whatever file that the functions are called.


I’m going to dm you my code just as an example because I don’t think I have a very solid grasp of how your code works. You’ll see that I’ve defined my functions, variables and devices in .cpp files and then extern is all in my .h files.

Okes so you can pretty much ignore my previous post, a lot of the errors were solved by updating vexcode :neutral_face:

All of the errors I found were solved by copying the function statements in driving.h to maneuvers.h (or just including the file works). Looks like you set everything up alright, but you just forgot to include your functions into maneuvers.h.

2 Likes

The source code for your project that you showed me helped a lot! Turns out I needed to put the externs in the PortConfig.h file, and DEFINE them in a PortConfig.cpp file. I didn’t have one previously. Thank you for the help.

also see this old topic.

VEXcode uses a conventional C++ toolchain. globals variables must only be declared once, using header guards or #pragma once in an included header does not attempt to address this requirement.

more tutorial material here.

https://www.learncpp.com/cpp-tutorial/42-global-variables/

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.