arduino / arduino/ArduinoCore-sam

USB_Host.h is missing extern "C" on function prototypes from uotghs_host.c

Open
#77 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
91
Forks
112
PR merge metrics
No merged PRs in 30d

Description

The prototypes in [USB_Host.h](https://github.com/arduino/Arduino/blob/master/hardware/arduino/sam/system/libsam/include/USB_host.h) for the functions from [uotghs_host.c](https://github.com/arduino/Arduino/blob/master/hardware/arduino/sam/system/libsam/source/uotghs_host.c) should be tagged as `extern "C"` since the header can be included into C++ source and is referencing C code. Without this, it will fail to locate the export names in libsam_sam3x8e_gcc_rel.a, since they are not C++ decorated.

In other words, it needs the following:

``` c++
#ifdef __cplusplus
extern "C" {
#endif

//extern uhd_speed_t uhd_get_speed(void);

extern void UHD_SetStack(void (*pf_isr)(void));
extern void UHD_Init(void);
extern void UHD_BusReset(void);
extern uhd_vbus_state_t UHD_GetVBUSState(void);
extern uint32_t UHD_Pipe0_Alloc(uint32_t ul_add, uint32_t ul_ep_size);
extern uint32_t UHD_Pipe_Alloc(uint32_t ul_dev_addr, uint32_t ul_dev_ep, uint32_t ul_type, uint32_t ul_dir, uint32_t ul_maxsize, uint32_t ul_interval, uint32_t ul_nb_bank);
extern void UHD_Pipe_Free(uint32_t ul_pipe);
extern uint32_t UHD_Pipe_Read(uint32_t ul_pipe, uint32_t ul_size, uint8_t* data);
extern void UHD_Pipe_Write(uint32_t ul_pipe, uint32_t ul_size, uint8_t* data);
extern void UHD_Pipe_Send(uint32_t ul_pipe, uint32_t ul_token_type);
extern uint32_t UHD_Pipe_Is_Transfer_Complete(uint32_t ul_pipe, uint32_t ul_token_type);

#ifdef __cplusplus
}
#endif
```

Without tagging them as `extern "C"`, you get errors such as the following, which I encountered while experimenting with adding USB Keyboard support to an Arduino Due project using the USBHost library:

```
./libarduino_due_x_dbg_myproj.a(myproj.cpp.obj): In function `HIDBoot<(unsigned char)1>::Release()':
myproj.cpp:(.text._ZN7HIDBootILh1EE7ReleaseEv[_ZN7HIDBootILh1EE7ReleaseEv]+0x28): warning: undefined reference to `UHD_Pipe_Free(unsigned long)'
./libarduino_due_x_dbg_myproj.a(myproj.cpp.obj): In function `HIDBoot<(unsigned char)1>::EndpointXtract(unsigned long, unsigned long, unsigned long, unsigned long, USB_ENDPOINT_DESCRIPTOR const*)':
myproj.cpp:(.text._ZN7HIDBootILh1EE14EndpointXtractEmmmmPK23USB_ENDPOINT_DESCRIPTOR[_ZN7HIDBootILh1EE14EndpointXtractEmmmmPK23USB_ENDPOINT_DESCRIPTOR]+0x60): warning: undefined reference to `UHD_Pipe_Alloc(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)'
```

I would have submitted this as a pull-request, but I've just been using the auto-downloaded library and haven't bothered forking it yet.

I don't know how the Arudino IDE deals with this problem, as I'm building via a combination of the command-line and QtCreator on Linux using CMake as my build system.

Contributor guide

No contributing guide indexed for this repository

Research direction

Open hardware/arduino/sam/system/libsam/include/USB_host.h and compare its UHD function prototypes with the C definitions in hardware/arduino/sam/system/libsam/source/uotghs_host.c. Add the C++ linkage guards around the prototypes, then verify that a C++ USBHost build resolves the symbols from libsam_sam3x8e_gcc_rel.a without decorated-name linker errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.