Skip to content

Commit

Permalink
pybricks: fix non-extern variables in header files
Browse files Browse the repository at this point in the history
In C, function definitions are implicitly extern, so the extern keyword
is not needed. But variables are not implicitly extern, so the extern
keyword is always needed.

Prior to GCC 10, the `-fcommon` flag was set by default which silently
ignored this mistake.

Fixed by adding extern everywhere. This also uncovered a bug where
`pybricks.parameters.Icon` was included on all hubs but would have
resulted in a crash when used on hubs where it should have been omitted.

Issue: pybricks/support#194
  • Loading branch information
dlech committed Jan 5, 2021
1 parent 14654bb commit 220afa2
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 110 deletions.
4 changes: 2 additions & 2 deletions bricks/ev3dev/pb_ev3dev_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ GrxFont *pb_ev3dev_Font_obj_get_font(mp_const_obj_t obj);

// class Image

const mp_obj_type_t pb_type_ev3dev_Image;
extern const mp_obj_type_t pb_type_ev3dev_Image;

// class Speaker

const mp_obj_type_t pb_type_ev3dev_Speaker;
extern const mp_obj_type_t pb_type_ev3dev_Speaker;

#endif // PYBRICKS_INCLUDED_BRICKS_EV3DEV_TYPES_H
10 changes: 5 additions & 5 deletions lib/pbio/include/pbio/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ typedef bool (*pbio_control_on_target_t)(pbio_trajectory_t *trajectory,
bool stalled);

// Functions to check whether motion is done
pbio_control_on_target_t pbio_control_on_target_always;
pbio_control_on_target_t pbio_control_on_target_never;
pbio_control_on_target_t pbio_control_on_target_angle;
pbio_control_on_target_t pbio_control_on_target_time;
pbio_control_on_target_t pbio_control_on_target_stalled;
extern pbio_control_on_target_t pbio_control_on_target_always;
extern pbio_control_on_target_t pbio_control_on_target_never;
extern pbio_control_on_target_t pbio_control_on_target_angle;
extern pbio_control_on_target_t pbio_control_on_target_time;
extern pbio_control_on_target_t pbio_control_on_target_stalled;

typedef enum {
PBIO_CONTROL_NONE, /**< No control */
Expand Down
14 changes: 7 additions & 7 deletions pybricks/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ mp_obj_t common_LightArray_obj_make_new(pb_device_t *pbdev, uint8_t light_mode,
#ifdef PYBRICKS_PY_COMMON_LIGHT_MATRIX
#include <pbio/light_matrix.h>
// pybricks._common.LightMatrix()
const uint8_t pb_digits_5x2[10][5];
const uint8_t pb_font_5x5[95][5];
extern const uint8_t pb_digits_5x2[10][5];
extern const uint8_t pb_font_5x5[95][5];
mp_obj_t pb_type_Lightmatrix_obj_new(pbio_light_matrix_t *light_matrix);
#endif

Expand All @@ -39,7 +39,7 @@ mp_obj_t pb_type_Keypad_obj_new(uint8_t number_of_buttons, const pb_obj_enum_mem
#endif

// pybricks._common.Battery()
const mp_obj_module_t pb_module_battery;
extern const mp_obj_module_t pb_module_battery;


#if PYBRICKS_PY_COMMON_MOTORS
Expand All @@ -48,7 +48,7 @@ const mp_obj_module_t pb_module_battery;
#include <pbio/servo.h>

// pybricks._common.Control()
const mp_obj_type_t pb_type_Control;
extern const mp_obj_type_t pb_type_Control;
mp_obj_t common_Control_obj_make_new(pbio_control_t *control);

// pybricks._common.Logger()
Expand All @@ -62,15 +62,15 @@ typedef struct _common_Motor_obj_t {
mp_obj_t logger;
} common_Motor_obj_t;

const mp_obj_type_t pb_type_Motor;
extern const mp_obj_type_t pb_type_Motor;

// pybricks._common.DCMotor()
typedef struct _common_DCMotor_obj_t {
mp_obj_base_t base;
pbio_dcmotor_t *dcmotor;
} common_DCMotor_obj_t;

const mp_obj_type_t pb_type_DCMotor;
extern const mp_obj_type_t pb_type_DCMotor;

// Nonstatic objects shared between Motor and DCMotor
void common_DCMotor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
Expand All @@ -82,7 +82,7 @@ MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_brake_obj);

#if PYBRICKS_PY_COMMON_SPEAKER

const mp_obj_type_t pb_type_Speaker;
extern const mp_obj_type_t pb_type_Speaker;

#endif // PYBRICKS_PY_COMMON_SPEAKER

Expand Down
12 changes: 6 additions & 6 deletions pybricks/ev3devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

#include "py/obj.h"

const mp_obj_module_t pb_module_ev3devices;
extern const mp_obj_module_t pb_module_ev3devices;

const mp_obj_type_t pb_type_ev3devices_ColorSensor;
const mp_obj_type_t pb_type_ev3devices_InfraredSensor;
const mp_obj_type_t pb_type_ev3devices_GyroSensor;
const mp_obj_type_t pb_type_ev3devices_TouchSensor;
const mp_obj_type_t pb_type_ev3devices_UltrasonicSensor;
extern const mp_obj_type_t pb_type_ev3devices_ColorSensor;
extern const mp_obj_type_t pb_type_ev3devices_InfraredSensor;
extern const mp_obj_type_t pb_type_ev3devices_GyroSensor;
extern const mp_obj_type_t pb_type_ev3devices_TouchSensor;
extern const mp_obj_type_t pb_type_ev3devices_UltrasonicSensor;

#endif // PYBRICKS_PY_EV3DEVICES

Expand Down
2 changes: 1 addition & 1 deletion pybricks/experimental.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "py/obj.h"

const mp_obj_module_t pb_module_experimental;
extern const mp_obj_module_t pb_module_experimental;

#endif // PYBRICKS_PY_EXPERIMENTAL

Expand Down
4 changes: 2 additions & 2 deletions pybricks/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

#include "py/obj.h"

const mp_obj_module_t pb_module_geometry;
extern const mp_obj_module_t pb_module_geometry;

const mp_obj_type_t pb_type_Matrix;
extern const mp_obj_type_t pb_type_Matrix;

typedef struct _pb_type_Matrix_obj_t {
mp_obj_base_t base;
Expand Down
16 changes: 8 additions & 8 deletions pybricks/hubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
#include "py/obj.h"

#if PYBRICKS_HUB_MOVEHUB
const mp_obj_type_t pb_type_MoveHub;
extern const mp_obj_type_t pb_type_MoveHub;
#endif
#if PYBRICKS_HUB_CITYHUB
const mp_obj_type_t pb_type_CityHub;
extern const mp_obj_type_t pb_type_CityHub;
#endif
#if PYBRICKS_HUB_TECHNICHUB
const mp_obj_type_t pb_type_TechnicHub;
extern const mp_obj_type_t pb_type_TechnicHub;
#endif
#ifdef PYBRICKS_HUB_PRIMEHUB
const mp_obj_type_t pb_type_PrimeHub;
const mp_obj_type_t pb_type_InventorHub;
extern const mp_obj_type_t pb_type_PrimeHub;
extern const mp_obj_type_t pb_type_InventorHub;
#endif
#if PYBRICKS_HUB_NXTBRICK
const mp_obj_type_t pb_type_NXTBrick;
extern const mp_obj_type_t pb_type_NXTBrick;
#endif
#if PYBRICKS_HUB_EV3BRICK
const mp_obj_type_t pb_type_EV3Brick;
extern const mp_obj_type_t pb_type_EV3Brick;
#endif

const mp_obj_module_t pb_module_hubs;
extern const mp_obj_module_t pb_module_hubs;

#endif // PYBRICKS_PY_HUBS

Expand Down
14 changes: 7 additions & 7 deletions pybricks/iodevices.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

#include "py/obj.h"

const mp_obj_module_t pb_module_iodevices;
extern const mp_obj_module_t pb_module_iodevices;

#if PYBRICKS_PY_PUPDEVICES

const mp_obj_type_t pb_type_iodevices_PUPDevice;
extern const mp_obj_type_t pb_type_iodevices_PUPDevice;

#endif // PYBRICKS_PY_PUPDEVICES

#if PYBRICKS_PY_EV3DEVICES

const mp_obj_type_t pb_type_iodevices_LUMPDevice;
const mp_obj_type_t pb_type_iodevices_AnalogSensor;
const mp_obj_type_t pb_type_iodevices_Ev3devSensor;
const mp_obj_type_t pb_type_iodevices_I2CDevice;
const mp_obj_type_t pb_type_iodevices_UARTDevice;
extern const mp_obj_type_t pb_type_iodevices_LUMPDevice;
extern const mp_obj_type_t pb_type_iodevices_AnalogSensor;
extern const mp_obj_type_t pb_type_iodevices_Ev3devSensor;
extern const mp_obj_type_t pb_type_iodevices_I2CDevice;
extern const mp_obj_type_t pb_type_iodevices_UARTDevice;

#endif // PYBRICKS_PY_EV3DEVICES

Expand Down
2 changes: 1 addition & 1 deletion pybricks/media.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "py/obj.h"

const mp_obj_module_t pb_module_media;
extern const mp_obj_module_t pb_module_media;

#endif // PYBRICKS_PY_MEDIA

Expand Down
18 changes: 9 additions & 9 deletions pybricks/nxtdevices.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

#include "py/obj.h"

const mp_obj_module_t pb_module_nxtdevices;

const mp_obj_type_t pb_type_nxtdevices_ColorSensor;
const mp_obj_type_t pb_type_nxtdevices_EnergyMeter;
const mp_obj_type_t pb_type_nxtdevices_LightSensor;
const mp_obj_type_t pb_type_nxtdevices_SoundSensor;
const mp_obj_type_t pb_type_nxtdevices_TemperatureSensor;
const mp_obj_type_t pb_type_nxtdevices_TouchSensor;
const mp_obj_type_t pb_type_nxtdevices_UltrasonicSensor;
extern const mp_obj_module_t pb_module_nxtdevices;

extern const mp_obj_type_t pb_type_nxtdevices_ColorSensor;
extern const mp_obj_type_t pb_type_nxtdevices_EnergyMeter;
extern const mp_obj_type_t pb_type_nxtdevices_LightSensor;
extern const mp_obj_type_t pb_type_nxtdevices_SoundSensor;
extern const mp_obj_type_t pb_type_nxtdevices_TemperatureSensor;
extern const mp_obj_type_t pb_type_nxtdevices_TouchSensor;
extern const mp_obj_type_t pb_type_nxtdevices_UltrasonicSensor;

int32_t analog_scale(int32_t mvolts, int32_t mvolts_min, int32_t mvolts_max, bool invert);

Expand Down
98 changes: 50 additions & 48 deletions pybricks/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@

#if PYBRICKS_PY_PARAMETERS_BUTTON

const mp_obj_type_t pb_enum_type_Button;

const pb_obj_enum_member_t pb_Button_UP_obj;
const pb_obj_enum_member_t pb_Button_DOWN_obj;
const pb_obj_enum_member_t pb_Button_LEFT_obj;
const pb_obj_enum_member_t pb_Button_RIGHT_obj;
const pb_obj_enum_member_t pb_Button_RIGHT_PLUS_obj;
const pb_obj_enum_member_t pb_Button_RIGHT_MINUS_obj;
const pb_obj_enum_member_t pb_Button_CENTER_obj;
const pb_obj_enum_member_t pb_Button_LEFT_UP_obj;
const pb_obj_enum_member_t pb_Button_LEFT_DOWN_obj;
const pb_obj_enum_member_t pb_Button_LEFT_PLUS_obj;
const pb_obj_enum_member_t pb_Button_LEFT_MINUS_obj;
const pb_obj_enum_member_t pb_Button_RIGHT_UP_obj;
const pb_obj_enum_member_t pb_Button_RIGHT_DOWN_obj;
const pb_obj_enum_member_t pb_Button_BEACON_obj;
const pb_obj_enum_member_t pb_Button_BLUETOOTH_obj;
extern const mp_obj_type_t pb_enum_type_Button;

extern const pb_obj_enum_member_t pb_Button_UP_obj;
extern const pb_obj_enum_member_t pb_Button_DOWN_obj;
extern const pb_obj_enum_member_t pb_Button_LEFT_obj;
extern const pb_obj_enum_member_t pb_Button_RIGHT_obj;
extern const pb_obj_enum_member_t pb_Button_RIGHT_PLUS_obj;
extern const pb_obj_enum_member_t pb_Button_RIGHT_MINUS_obj;
extern const pb_obj_enum_member_t pb_Button_CENTER_obj;
extern const pb_obj_enum_member_t pb_Button_LEFT_UP_obj;
extern const pb_obj_enum_member_t pb_Button_LEFT_DOWN_obj;
extern const pb_obj_enum_member_t pb_Button_LEFT_PLUS_obj;
extern const pb_obj_enum_member_t pb_Button_LEFT_MINUS_obj;
extern const pb_obj_enum_member_t pb_Button_RIGHT_UP_obj;
extern const pb_obj_enum_member_t pb_Button_RIGHT_DOWN_obj;
extern const pb_obj_enum_member_t pb_Button_BEACON_obj;
extern const pb_obj_enum_member_t pb_Button_BLUETOOTH_obj;

#endif // PYBRICKS_PY_PARAMETERS_BUTTON

const mp_obj_type_t pb_type_Color;
extern const mp_obj_type_t pb_type_Color;

typedef struct _pb_type_Color_obj_t {
mp_obj_base_t base;
Expand All @@ -47,45 +47,47 @@ typedef struct _pb_type_Color_obj_t {
pb_type_Color_obj_t *pb_type_Color_new_empty(void);
const pbio_color_hsv_t *pb_type_Color_get_hsv(mp_obj_t obj);

const pb_type_Color_obj_t pb_Color_RED_obj;
const pb_type_Color_obj_t pb_Color_BROWN_obj;
const pb_type_Color_obj_t pb_Color_ORANGE_obj;
const pb_type_Color_obj_t pb_Color_YELLOW_obj;
const pb_type_Color_obj_t pb_Color_GREEN_obj;
const pb_type_Color_obj_t pb_Color_CYAN_obj;
const pb_type_Color_obj_t pb_Color_BLUE_obj;
const pb_type_Color_obj_t pb_Color_VIOLET_obj;
const pb_type_Color_obj_t pb_Color_MAGENTA_obj;
const pb_type_Color_obj_t pb_Color_NONE_obj;
const pb_type_Color_obj_t pb_Color_BLACK_obj;
const pb_type_Color_obj_t pb_Color_GRAY_obj;
const pb_type_Color_obj_t pb_Color_WHITE_obj;
extern const pb_type_Color_obj_t pb_Color_RED_obj;
extern const pb_type_Color_obj_t pb_Color_BROWN_obj;
extern const pb_type_Color_obj_t pb_Color_ORANGE_obj;
extern const pb_type_Color_obj_t pb_Color_YELLOW_obj;
extern const pb_type_Color_obj_t pb_Color_GREEN_obj;
extern const pb_type_Color_obj_t pb_Color_CYAN_obj;
extern const pb_type_Color_obj_t pb_Color_BLUE_obj;
extern const pb_type_Color_obj_t pb_Color_VIOLET_obj;
extern const pb_type_Color_obj_t pb_Color_MAGENTA_obj;
extern const pb_type_Color_obj_t pb_Color_NONE_obj;
extern const pb_type_Color_obj_t pb_Color_BLACK_obj;
extern const pb_type_Color_obj_t pb_Color_GRAY_obj;
extern const pb_type_Color_obj_t pb_Color_WHITE_obj;

const mp_obj_type_t pb_enum_type_Direction;
extern const mp_obj_type_t pb_enum_type_Direction;

const pb_obj_enum_member_t pb_Direction_CLOCKWISE_obj;
const pb_obj_enum_member_t pb_Direction_COUNTERCLOCKWISE_obj;
extern const pb_obj_enum_member_t pb_Direction_CLOCKWISE_obj;
extern const pb_obj_enum_member_t pb_Direction_COUNTERCLOCKWISE_obj;

const mp_obj_base_t pb_Icon_obj;
#if PYBRICKS_PY_PARAMETERS_ICON
extern const mp_obj_base_t pb_Icon_obj;
#endif

const mp_obj_type_t pb_enum_type_Port;
extern const mp_obj_type_t pb_enum_type_Port;

const mp_obj_type_t pb_enum_type_Stop;
extern const mp_obj_type_t pb_enum_type_Stop;

const pb_obj_enum_member_t pb_Stop_COAST_obj;
const pb_obj_enum_member_t pb_Stop_BRAKE_obj;
const pb_obj_enum_member_t pb_Stop_HOLD_obj;
extern const pb_obj_enum_member_t pb_Stop_COAST_obj;
extern const pb_obj_enum_member_t pb_Stop_BRAKE_obj;
extern const pb_obj_enum_member_t pb_Stop_HOLD_obj;

const mp_obj_type_t pb_enum_type_Side;
extern const mp_obj_type_t pb_enum_type_Side;

const pb_obj_enum_member_t pb_Side_BACK_obj;
const pb_obj_enum_member_t pb_Side_BOTTOM_obj;
const pb_obj_enum_member_t pb_Side_FRONT_obj;
const pb_obj_enum_member_t pb_Side_LEFT_obj;
const pb_obj_enum_member_t pb_Side_RIGHT_obj;
const pb_obj_enum_member_t pb_Side_TOP_obj;
extern const pb_obj_enum_member_t pb_Side_BACK_obj;
extern const pb_obj_enum_member_t pb_Side_BOTTOM_obj;
extern const pb_obj_enum_member_t pb_Side_FRONT_obj;
extern const pb_obj_enum_member_t pb_Side_LEFT_obj;
extern const pb_obj_enum_member_t pb_Side_RIGHT_obj;
extern const pb_obj_enum_member_t pb_Side_TOP_obj;

const mp_obj_module_t pb_module_parameters;
extern const mp_obj_module_t pb_module_parameters;

#endif // PYBRICKS_PY_PARAMETERS

Expand Down
2 changes: 2 additions & 0 deletions pybricks/parameters/pb_module_parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ STATIC const mp_rom_map_elem_t parameters_globals_table[] = {
#endif
{ MP_ROM_QSTR(MP_QSTR_Color), MP_ROM_PTR(&pb_type_Color) },
{ MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&pb_enum_type_Direction) },
#if PYBRICKS_PY_PARAMETERS_ICON
{ MP_ROM_QSTR(MP_QSTR_Icon), MP_ROM_PTR(&pb_Icon_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_Port), MP_ROM_PTR(&pb_enum_type_Port) },
{ MP_ROM_QSTR(MP_QSTR_Side), MP_ROM_PTR(&pb_enum_type_Side) },
{ MP_ROM_QSTR(MP_QSTR_Stop), MP_ROM_PTR(&pb_enum_type_Stop) },
Expand Down
20 changes: 10 additions & 10 deletions pybricks/pupdevices.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

#include <pybricks/util_pb/pb_device.h>

const mp_obj_module_t pb_module_pupdevices;

const mp_obj_type_t pb_type_pupdevices_ColorDistanceSensor;
const mp_obj_type_t pb_type_pupdevices_ColorSensor;
const mp_obj_type_t pb_type_pupdevices_ForceSensor;
const mp_obj_type_t pb_type_pupdevices_InfraredSensor;
const mp_obj_type_t pb_type_pupdevices_Light;
const mp_obj_type_t pb_type_pupdevices_PFMotor;
const mp_obj_type_t pb_type_pupdevices_TiltSensor;
const mp_obj_type_t pb_type_pupdevices_UltrasonicSensor;
extern const mp_obj_module_t pb_module_pupdevices;

extern const mp_obj_type_t pb_type_pupdevices_ColorDistanceSensor;
extern const mp_obj_type_t pb_type_pupdevices_ColorSensor;
extern const mp_obj_type_t pb_type_pupdevices_ForceSensor;
extern const mp_obj_type_t pb_type_pupdevices_InfraredSensor;
extern const mp_obj_type_t pb_type_pupdevices_Light;
extern const mp_obj_type_t pb_type_pupdevices_PFMotor;
extern const mp_obj_type_t pb_type_pupdevices_TiltSensor;
extern const mp_obj_type_t pb_type_pupdevices_UltrasonicSensor;

pb_device_t *pupdevices_ColorDistanceSensor__get_device(mp_obj_t obj);

Expand Down
4 changes: 2 additions & 2 deletions pybricks/robotics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include "py/obj.h"


const mp_obj_type_t pb_type_drivebase;
extern const mp_obj_type_t pb_type_drivebase;

const mp_obj_module_t pb_module_robotics;
extern const mp_obj_module_t pb_module_robotics;

#endif // PYBRICKS_PY_ROBOTICS

Expand Down
Loading

0 comments on commit 220afa2

Please sign in to comment.