errors with old project starting mbed ver 40

10 Sep 2012

After some time i reopen one of my projects which is using the old network stack. I update the mbed lib and run into some errors. Starting with version 40 i get some undefined symbols by linking the NetServices. dns_setver() ...

11 Sep 2012

Hi Peter,

This is likely related to a change we made to the compiler when it is using v40 onwards.

Historically, we forced all files to be compiled as cpp, whether the extension was .c or .cpp. This was originally meant to avoid confusion with c and cpp linkage (i.e. name mangling), but as people have started importing large existing C codebases, it turns out this non-standard behaviour was a mistake. As such, from v40 onwards, we now compile .c files as C, and .cpp files as C++, providing a more standard behaviour.

This is almost certainly the reason you are getting undefined symbols; you have some functions that in C files that are being called by C++ files, but the headers don't define the functions as extern "C", so they don't match up at link time.

What you generally want to do to fix this is:

  • Rename .c files as .cpp, so they get compiled as C++ as before
  • Mark the functions in .h headers of .c files as being C linkage, so when included by C++ files you get the right result. i.e:
// my C .h header file

#ifdef __cplusplus 
extern "C" {
#endif

void my_c_function();

#ifdef __cplusplus
}
#endif

For more about name mangling, see:

Hope that helps!

Simon

02 Oct 2012

I'm still getting linker errors when using old C++-based libraries.

Here's an easy-to-replicate example...

1. Create a new project. 2. Import this library: http://mbed.org/users/segundo/code/NetServices/ 3. Attempt to compile.

I still have a project that uses version 39, and this library compiles just fine. I'm afraid to upgrade because it'll almost certainly break it.