1

I am going to take a hit here because I am using tools I don't fully understand and haven't taken the time to do so. I just want to remove unnecessary steps / dependencies from my work quickly at this point.

I was using AddressSanitizer to find the cause of segfaults and other issues in my C code during development. I did this by adding -fsanitize=address to my list of CFLAGS in a Makefile.

Now, having fixed all such issues, if I remove this flag from my compilation, I can no longer compile my code, and get many errors much like those described in this question...

How to use AddressSanitizer with GCC?

~/xxx/util.c:20: undefined reference to __asan_option_detect_stack_use_after_return' /usr/bin/ld: ~/xxx/util.c:20: undefined reference to __asan_stack_malloc_2' usr/bin/ld: /xxx/util.c:23: undefined reference to __asan_report_store8' /usr/bin/ld: /xxx/util.c:40: undefined reference to __asan_report_load8' /usr/bin/ld: /xxx/util.c:45: undefined reference to `__asan_report_load8' /usr/bin/ld: And so on.

The difference is I just want to compile my code normally. In that question, they suggest adding a flag -lasan to link/compile step. That doesn't seem like it will help me to stop using -fsanitize, though I could be wrong. Obviously something is wrong in the linking step here. It feels like I have included a dependency that I forgot about somewhere, but I don't recall doing that.

What am I missing? Probably something obvious. Also, let me know if I need to give more info about my compilation step.

Brian Peterson
  • 2,800
  • 6
  • 29
  • 36

2 Answers2

6

The problem is simple: for it to work, -fsanitize=address must be present in both compilation and linking command lines. To disable it, it must not be used in either.

You must have old .o object files in your build directory. One solution would be to use make clean to remove the intermediate or target files.

If you do not have the clean target, now would be good time to write it... or use something like find -name '*.o' -delete or rm **/*.o if fortunate enough to use ZSH.

  • It sounds to me that it's time for Brian to start using a tool such as `cmake` which 99.999% of the time automatically takes care of issues like these (i.e. `cmake` copies the flags to both places as required, it rebuilds the targets if/when necessary, and it gives you a free `clean` target...) – Alexis Wilke Jul 15 '21 at 22:30
3

I just want to compile my code normally

Removing the -fsanitize=... flag should do exactly that.

Your problem most likely stems from not rebuilding all the objects (when you update Makefile, make doesn't automatically know to rebuild everything).

Running make clean; make or find . -name '*.o' | xargs rm; make should do the trick.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 2
    note that it is possible to set up the Makefile so that it does rebuild everything when the Makefile changes (OP obviously has not done that) – M.M Nov 01 '20 at 20:58