Go Down

Topic: problem when using headers (Read 129 times) previous topic - next topic

tanzak

Hello,

i'm trying to Split my code in headers and different files, but it doesn't work with a function that returns a long variable.

the header:

#ifndef HCSR04_H_
#define HCSR04_H_

class HCSR04{
public:
    long distObstacle();
};


#endif /* HCSR04_H_ */


the cpp file associated:

#include <SPI.h>
#include <Wire.h>
#include <Stdio.h>
#include "HCSR04.h"
#include "config.h"


//function that measures the distance with the distance sensor
long HCSR04::distObstacle(void) {

     long duration, distance;
     digitalWrite(trigPin, LOW);        // sets the trigger to LOW
     delayMicroseconds(2);              // wait 2 microseconds
     digitalWrite(trigPin, HIGH);       // sets the trigger to HIGH

     delayMicroseconds(10);             // wait 10 microseconds
     digitalWrite(trigPin, LOW);        // sets the trigger to LOW
     duration = pulseIn(echoPin, HIGH); // counts the time to echo pulse
     distance = (duration/2) / 29.1;    // calculates the distance to the obstacle in cm
     delay(500);
     return distance;
}

the main code (where the header is included, variable initialized and first use:


#include "HCSR04.h"

HCSR04 myDistSensor;

long distToObst;

void setup() {

...

}

void loop() {
   distToObst=myDistSensor.distObstacle();    //distance to obstacle
}


I get the next error in the cpp file:

first defined here (at line "long HCSR04::distObstacle(void)")


I've searched for several examples and it seems that the code is right. Besides, I'm using the same structure for another sensor (but without returning variable, that is with 'void'), and it Works perfectly, so I guess it has to be with the scope of the variable. but i've tried using extern, and it does not work either...any idea of what is wrong?

pert

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]// your code is here[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

We need the full error output, not just that fragment.
When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

tanzak

ok, sorry. I repeat the message following your directions:

Hello,

i'm trying to Split my code in headers and different files, but it doesn't work with a function that returns a long variable.

the header:

Code: [Select]

#ifndef HCSR04_H_
#define HCSR04_H_

class HCSR04{
public:
    long distObstacle();
};


#endif /* HCSR04_H_ */


the cpp file associated:

Code: [Select]

#include <SPI.h>
#include <Wire.h>
#include <Stdio.h>
#include "HCSR04.h"
#include "config.h"


//function that measures the distance with the distance sensor
long HCSR04::distObstacle(void) {

     long duration, distance;
     digitalWrite(trigPin, LOW);        // sets the trigger to LOW
     delayMicroseconds(2);              // wait 2 microseconds
     digitalWrite(trigPin, HIGH);       // sets the trigger to HIGH

     delayMicroseconds(10);             // wait 10 microseconds
     digitalWrite(trigPin, LOW);        // sets the trigger to LOW
     duration = pulseIn(echoPin, HIGH); // counts the time to echo pulse
     distance = (duration/2) / 29.1;    // calculates the distance to the obstacle in cm
     delay(500);
     return distance;
}


the main code (where the header is included, variable initialized and first use:


Code: [Select]

#include "HCSR04.h"

HCSR04 myDistSensor;

long distToObst;

void setup() {

...

}

void loop() {
   distToObst=myDistSensor.distObstacle();    //distance to obstacle
}



I get the next error in the cpp file:

Code: [Select]
first defined here
 (at line "long HCSR04::distObstacle(void)")


I've searched for several examples and it seems that the code is right. Besides, I'm using the same structure for another sensor (but without returning variable, that is with 'void'), and it Works perfectly, so I guess it has to be with the scope of the variable. but i've tried using extern, and it does not work either...any idea of what is wrong?

pert

Thanks for using code tags, but you forgot to follow the other part of my reply:
We need the full error output, not just that fragment.
When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

tanzak

The error report I see is:

Code: [Select]

**** Build of configuration Debug for project MotorController-viaWebServerWiFi_v4 ****

"C:\\texas_instruments\\ccsv7\\utils\\bin\\gmake" -k -j 4 all -O
 
'Building file: "../HCSR04.cpp"'
'Invoking: GNU Compiler'
"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/tools/arm-none-eabi-gcc/4.8.4-20140725/bin/arm-none-eabi-gcc.exe" -c -mcpu=cortex-m4 -march=armv7e-m -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fno-exceptions -Dxdc_target_types__=gnu/targets/arm/std.h -Dxdc_target_name__=M4F -Dxdc_cfg__xheader__=\"configPkg/package/cfg/energia_pm4fg.h\" -DTARGET_IS_MSP432P4XX -DBOARD_MSP_EXP432P401R -Dxdc__nolocalstring=1 -D__MSP432P401R__ -DCORE_VERSION=380 -DF_CPU=48000000L -DENERGIA_MSP_EXP432P401R -DENERGIA_ARCH_MSP432 -DENERGIA=18 -DARDUINO=10610 -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432/ti/runtime/wiring/" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432/ti/runtime/wiring/msp432" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/system/driverlib/MSP432P4xx" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/system/driverlib" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/system/inc" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/system/inc/CMSIS/" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/system" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/variants/MSP_EXP432P401R" -I"C:/Users/Xavi/workspace_v7/MotorController-viaWebServerWiFi_v4" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/libraries/WiFi" -I"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/tools/arm-none-eabi-gcc/4.8.4-20140725/arm-none-eabi/include" -Os -ffunction-sections -fdata-sections -g -gdwarf-3 -gstrict-dwarf -w -Wall -mabi=aapcs -fno-threadsafe-statics -MMD -MP -MF"HCSR04.d" -MT"HCSR04.o" -fno-rtti  -o"HCSR04.o" "../HCSR04.cpp"
'Finished building: "../HCSR04.cpp"'
 
'Building target: "MotorController-viaWebServerWiFi_v4.out"'
'Invoking: GNU Linker'
"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/tools/arm-none-eabi-gcc/4.8.4-20140725/bin/arm-none-eabi-gcc.exe" -fno-exceptions -Dxdc_target_types__=gnu/targets/arm/std.h -Dxdc_target_name__=M4F -Dxdc_cfg__xheader__=\"configPkg/package/cfg/energia_pm4fg.h\" -DTARGET_IS_MSP432P4XX -DBOARD_MSP_EXP432P401R -Dxdc__nolocalstring=1 -D__MSP432P401R__ -DCORE_VERSION=380 -DF_CPU=48000000L -DENERGIA_MSP_EXP432P401R -DENERGIA_ARCH_MSP432 -DENERGIA=18 -DARDUINO=10610 -Os -ffunction-sections -fdata-sections -g -gdwarf-3 -gstrict-dwarf -w -Wall -mabi=aapcs -fno-threadsafe-statics -march=armv7e-m -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,-Map,"MotorController-viaWebServerWiFi_v4.map" -nostartfiles -Wl,--gc-sections -L"C:/Users/Xavi/workspace_v7/msp432_MSP-EXP432P401R_WiFi/Debug" -L"C:/Users/Xavi/workspace_v7/msp432_MSP-EXP432P401R_core/Debug" -L"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432" -L"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432/ti/runtime/wiring/msp432" -L"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432/ti/runtime/wiring/msp432/variants/MSP_EXP432P401R" -L"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432/gnu/targets/arm/libs/install-native/arm-none-eabi/lib/armv7e-m/fpu" -L"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/system/driverlib/MSP432P4xx/gcc" -Wl,--undefined,main -Os -Wl,--check-sections -o"MotorController-viaWebServerWiFi_v4.out" "./HCSR04.o" "./main.o" "./MotorController-viaWebServerWiFi_v3.1.o" "./SCMD.o"  -Wl,--start-group -Wl,-T"C:/Users/Xavi/AppData/Local/Energia15/packages/energia/hardware/msp432/3.8.0/cores/msp432/ti/runtime/wiring/msp432/linker.cmd" -lmsp432_MSP-EXP432P401R_WiFi -lmsp432_MSP-EXP432P401R_core -l:msp432p4xx_driverlib.a -lstdc++ -lgcc -lc -lm -lnosys -Wl,--end-group
makefile:157: recipe for target 'MotorController-viaWebServerWiFi_v4.out' failed
./MotorController-viaWebServerWiFi_v3.1.o: In function `TwoWire::write(int)':
C:/Users/Xavi/workspace_v7/MotorController-viaWebServerWiFi_v4/MotorController-viaWebServerWiFi_v3.1.ino:310: multiple definition of `serverObj'
./HCSR04.o:C:\Users\Xavi\workspace_v7\MotorController-viaWebServerWiFi_v4\Debug/../HCSR04.cpp:13: first defined here
./MotorController-viaWebServerWiFi_v3.1.o:(.data.port+0x0): multiple definition of `port'
./HCSR04.o:(.data.port+0x0): first defined here
./MotorController-viaWebServerWiFi_v3.1.o:(.data.password+0x0): multiple definition of `password'
./HCSR04.o:(.data.password+0x0): first defined here
./MotorController-viaWebServerWiFi_v3.1.o:(.data.ssid+0x0): multiple definition of `ssid'
./HCSR04.o:(.data.ssid+0x0): first defined here
./SCMD.o:(.bss.serverObj+0x0): multiple definition of `serverObj'
./HCSR04.o:C:\Users\Xavi\workspace_v7\MotorController-viaWebServerWiFi_v4\Debug/../HCSR04.cpp:13: first defined here
./SCMD.o:(.data.port+0x0): multiple definition of `port'
./HCSR04.o:(.data.port+0x0): first defined here
./SCMD.o:(.data.password+0x0): multiple definition of `password'
./HCSR04.o:(.data.password+0x0): first defined here
./SCMD.o:(.data.ssid+0x0): multiple definition of `ssid'
./HCSR04.o:(.data.ssid+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
gmake[1]: *** [MotorController-viaWebServerWiFi_v4.out] Error 1
gmake: *** [all] Error 2
makefile:149: recipe for target 'all' failed

**** Build Finished ****

pert

Where are serverObj, port, password, and ssid defined? Maybe in config.h? You didn't post that code.

tanzak

Yes, they are defined in config.h:


Code: [Select]

#include <WiFi.h>
#include <WiFiClient.h>

#ifndef CONFIG_H_
#define CONFIG_H_

// configuration for WIFI connection
extern char ssid[] = "MOVISTAR_9";          // your network name also called SSID
extern char password[] = "myPass";   // your network password

// configuration of the server where the data is sent
extern uint16_t port = 80;     // port number of the server
extern IPAddress serverObj(192, 168, 1, 41);   // IP Address of the server


// definition of pins used by the distance sensor HCSR04
#define trigPin 38 //pin where the trigger for the distance sensor is attached
#define echoPin 37  //pin where the echo from the distance sensor is attached

// definition of constants used by the SCMD driver
#define FORWARD 1    //definition of constant FORWARD=1
#define BACKWARD 2  //definition of constant BACKWARD=2
#define RIGHT 3     //definition of constant RIGHT=3
#define LEFT 4      //definition of constant LEFT=4
#define STOP 0      //definition of constant STOP=0
#define addressI2C 0x59  //address I2C associated to the SCMD

// definition of pins for LEDs
#define onLED 75  //LED that indicates if the robot is scanning
#define leftContLED 39  //LED that indicates a left contact
#define rightContLED 40   //LED that indicates a right contact

//definition of pins for the contact sensors
#define leftContactPIN 39 //left contact sensor on pin P2.6
#define rightContactPIN 40 //right contact sensor on pin P2.7



#endif /* CONFIG_H_ */

tanzak

I don't know if i'm right, but it looks like the problem is that I include the header "config.h" in two classes functions: SCMD.cpp and HCSR04.cpp. Could be this the problem? if so, can't I use a general header for configurations used in the whole Project? how could I do it?

johnwasser

Now that you have provided config.h, I don't get any errors, just a bunch of warnings.

Your error point to multiple definitions of "ssid" and "ssid" is declared in config.h so if config.h gets included in more than one file it will cause such errors.

One of the warnings is that you are using the 'extern' keyword on ssid but also initializing the array.  You should not be doing both.   Move the initialization into one of the source files (.cpp or .ino) and just leave the 'extern' declaration in the header.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

tanzak

Ohh thanks! now it Works!

regarding the second recommendation, as I wanted to keep the configuration of the server's IP in the config file, I have created a constant with #define there, and the initialization is done in the main file.

Thank you very much for your help.

Go Up