Although we fairly recently added these documentation pages, it’s good to reiterate some of the best practices.
In this particular case, it’s about using std::string
and knowing about the downsides to it.
Apart from added compile time, it also adds extra code bloat (as C++ templates does) and is slower than C strings.
Those are side effects you can notice yourself, but there’s another more sneaky part to it:
An API is basically the “names” of structs and functions:
struct Foo;
Foo* Alloc();
void DoWork(Foo*);
And ABI is the Application Binary Interface
, which means how the structs are actually implemented internally.
// library version 1
struct Foo {
uint8_t data;
};
// library version 2
struct Foo {
uint32_t data;
};
(This is a simplified case just to illustrate the point)
If two different libraries are built towards two different versions of the ABI, and you later want to use both libraries in your executable, you can get nasty issues. At best a linker error. Or you can get a runtime error or possibly a crash (because then you at least know about it!), or at worst, a hidden bug lingering on.
In this case, we didn’t get a linker error, but sure enough, the GCC version wasn’t supported on the platform in question (SteamOS):
symbol _ZTVNSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEEE
version GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference
In the Defold engine itself, we stay away from the problems by simply using C strings:
#include <string.h>
#include <stdio.h>
size_t strlen(const char *s)
int snprintf(char *str, size_t size, const char *format, …)
char *strncat(char *dest, const char *src, size_t n)
So, now you know a little more about the potential issues when using STL
and std::
functions
and how to avoid them.