Skip to content

Instantly share code, notes, and snippets.

@raek
Created September 21, 2016 12:55
Show Gist options
  • Save raek/ed6a615fe9eefa0345037c3465a5e38c to your computer and use it in GitHub Desktop.
Save raek/ed6a615fe9eefa0345037c3465a5e38c to your computer and use it in GitHub Desktop.
unused symbols and GCC
#include <stdio.h>
int used(void)
{
printf("all used\n");
return 0;
}
int unused(void)
{
printf("all unused\n");
return 9000;
}
int unrelated(void)
{
return 42;
}
int main(int argc, char **argv)
{
return used();
}
#!/usr/bin/env bash
# Compile
gcc -o all.o -c all.c
gcc -o main.o -c main.c
gcc -o helper.o -c helper.c
gcc -o unrelated.o -c unrelated.c
# Archive
rm -f helper.a # ar has no option to completely overwrite existing file
ar rcf helper.a helper.o
rm -f unrelated.a
ar rcf unrelated.a unrelated.o
# Link
gcc -o test1 all.o
gcc -o test2 main.o helper.o unrelated.o
gcc -o test3 main.o helper.a unrelated.a
# Tests
echo Same object file
nm test1 | grep "\b\(unused\|used\|unrelated\)\b"
echo Separate object file
nm test2 | grep "\b\(unused\|used\|unrelated\)\b"
echo Separate archived object file
nm test3 | grep "\b\(unused\|used\|unrelated\)\b"
#include <stdio.h>
int used(void)
{
printf("used\n");
return 0;
}
int unused(void)
{
printf("unused\n");
return 9000;
}
int used(void);
int main(int argc, char **argv)
{
return used();
}
Same object file
0000000000400550 T unrelated
000000000040053b T unused
0000000000400526 T used
Separate object file
0000000000400566 T unrelated
0000000000400551 T unused
000000000040053c T used
Separate archived object file
0000000000400551 T unused
000000000040053c T used
int unrelated(void)
{
return 42;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment