Read a target triple
Split a toolchain prefix into architecture, vendor, system and ABI. An easy embedded C problem (Toolchains and cross-compilation), run and graded in the…
easy · Toolchains and cross-compilation · toolchains · strings · parsing
Cross-compilers are named by their target triple (which, despite the name, has three or four parts): arm-none-eabi-gcc targets arm with no OS and the embedded ABI.
Write parse_triple(s, t), which fills a triple_t from a triple of the form arch-vendor-system-abi or arch-system-abi (three parts: no vendor). Each part has at most 15 characters. Use "unknown" for a missing vendor. Return the number of parts found, or 0 if there are fewer than 3 or more than 4.
Starter code
int parse_triple(const char *s, triple_t *t) {
char parts[4][16] = {{0}};
int n = 0, k = 0;
for (; *s && n < 4; s++) {
if (*s == '-') { n++; k = 0; }
else if (k < 15) parts[n][k++] = *s;
}
strcpy(t->arch, parts[0]); strcpy(t->vendor, parts[1]); strcpy(t->sys, parts[2]); strcpy(t->abi, parts[3]);
return n + 1;
}Example tests
Four parts
show("aarch64-unknown-linux-gnu");
show("arm-unknown-linux-gnueabihf");Expected output:
aarch64-unknown-linux-gnu: arch=aarch64 vendor=unknown system=linux abi=gnu
arm-unknown-linux-gnueabihf: arch=arm vendor=unknown system=linux abi=gnueabihfThree parts
show("arm-none-eabi");
show("x86_64-linux-gnu");Expected output:
arm-none-eabi: arch=arm vendor=unknown system=none abi=eabi
x86_64-linux-gnu: arch=x86_64 vendor=unknown system=linux abi=gnuLearn it first
- Toolchains and cross-compilation · Target triplets, sysroots, float ABI and the flags that must agree.