Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Assert that some code is not present in the final binary, at compile or link time.

Calling an undefined function will have that behavior at link time: landmine.c: #ifndef CONSTANT #define CONSTANT 0 #endif #define assert_not_in_binary_if(e) do \ { \ if (e) \ undefi...

posted 2y ago by alx‭  ·  edited 2y ago by alx‭

Answer
#11: Post edited by user avatar alx‭ · 2022-06-27T09:24:11Z (almost 2 years ago)
tfix
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define assert_not_present_in_binary_if(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • assert_not_present_in_binary_if(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • assert_not_present_in_binary_if(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the assertion, and it is `foo0()` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time assertion, but link-time is good enough.
  • *EDITED: renamed `landmine()` to `assert_not_present_in_binary_if()`, per Dirk's suggestion.*
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define assert_not_in_binary_if(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • assert_not_in_binary_if(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • assert_not_in_binary_if(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the assertion, and it is `foo0()` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time assertion, but link-time is good enough.
  • *EDITED: renamed `landmine()` to `assert_not_in_binary_if()`, per Dirk's suggestion.*
#10: Post edited by user avatar alx‭ · 2022-06-27T09:23:00Z (almost 2 years ago)
renamed landmine() to assert_not_present_in_binary_if(), per Dirk's suggestion.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define landmine(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • landmine(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • landmine(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo0` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link-time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define assert_not_present_in_binary_if(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • assert_not_present_in_binary_if(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • assert_not_present_in_binary_if(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the assertion, and it is `foo0()` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time assertion, but link-time is good enough.
  • *EDITED: renamed `landmine()` to `assert_not_present_in_binary_if()`, per Dirk's suggestion.*
#9: Post edited by user avatar Dirk Herrmann‭ · 2022-06-24T14:52:52Z (almost 2 years ago)
Small lexical error
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define landmine(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • landmine(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • landmine(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo(0)` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link-time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define landmine(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • landmine(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • landmine(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo0` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link-time is good enough.
#8: Post edited by user avatar alx‭ · 2022-06-23T14:50:28Z (almost 2 years ago)
#7: Post edited by user avatar alx‭ · 2022-06-23T14:48:04Z (almost 2 years ago)
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccKhd1cW.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo(0)` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link-time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • #define landmine(e) do \
  • { \
  • if (e) \
  • undefined_function(); \
  • } while (0)
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int main(void)
  • {
  • CONSTANT ? foo1() : foo0();
  • }
  • static void foo0(void)
  • {
  • landmine(!CONSTANT);
  • }
  • static void foo1(void)
  • {
  • landmine(!CONSTANT);
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccmIFcBz.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo(0)` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link-time is good enough.
#6: Post edited by user avatar alx‭ · 2022-06-23T13:46:32Z (almost 2 years ago)
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccKhd1cW.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo(0)` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccKhd1cW.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo(0)` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link-time is good enough.
#5: Post edited by user avatar alx‭ · 2022-06-23T13:43:54Z (almost 2 years ago)
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccKhd1cW.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c </pre>
  • It would be nicer to have a compile-time landmine, but link time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccKhd1cW.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c </pre>
  • I used `[[gnu::noipa]]` just to check which function is triggering the landmine, and it is `foo(0)` as expected, and only when `CONSTANT == 0`.
  • It would be nicer to have a compile-time landmine, but link time is good enough.
#4: Post edited by user avatar alx‭ · 2022-06-23T13:39:41Z (almost 2 years ago)
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #define CONSTANT 0
  • void undefined_function(void);
  • static void foo0(void);
  • static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre>$ cc -Wall -Wextra -O3 landmine.c
  • /usr/bin/ld: /tmp/ccH3ypC6.o: in function `main&apos;:
  • landmine.c:(.text.startup+0x5): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • <pre>$ clang -flto -Weverything -O3 landmine.c
  • /usr/bin/ld: /tmp/lto-llvm-50c0c6.o: in function `main&apos;:
  • ld-temp.o:(.text.main+0x2): undefined reference to `undefined_function&apos;
  • clang: <font color="#FF5555"><b>error: </b></font><b>linker command failed with exit code 1 (use -v to see invocation)</b></pre>
  • It would be nicer to have a compile-time landmine, but link time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #ifndef CONSTANT
  • #define CONSTANT 0
  • #endif
  • void undefined_function(void);
  • [[gnu::noipa]] static void foo0(void);
  • [[gnu::noipa]] static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre><font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=0 landmine.c
  • /usr/bin/ld: /tmp/ccKhd1cW.o: in function `foo0&apos;:
  • landmine.c:(.text+0x1): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • <font color="#55FF55"><b>alx@asus5775</b></font>:<font color="#5555FF"><b>~/tmp</b></font>$ cc -Wall -Wextra -O3 -DCONSTANT=1 landmine.c </pre>
  • It would be nicer to have a compile-time landmine, but link time is good enough.
#3: Post edited by user avatar alx‭ · 2022-06-23T13:34:17Z (almost 2 years ago)
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #define CONSTANT 0
  • void undefined_function(void);
  • static void foo0(void);
  • static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre>$ cc -Wall -Wextra -O3 landmine.c
  • /usr/bin/ld: /tmp/ccH3ypC6.o: in function `main&apos;:
  • landmine.c:(.text.startup+0x5): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • $ clang -flto -Weverything -O3 landmine.c
  • /usr/bin/ld: /tmp/lto-llvm-50c0c6.o: in function `main':
  • ld-temp.o:(.text.main+0x2): undefined reference to `undefined_function'
  • clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • It would be nicer to have a compile-time landmine, but link time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #define CONSTANT 0
  • void undefined_function(void);
  • static void foo0(void);
  • static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre>$ cc -Wall -Wextra -O3 landmine.c
  • /usr/bin/ld: /tmp/ccH3ypC6.o: in function `main&apos;:
  • landmine.c:(.text.startup+0x5): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • <pre>$ clang -flto -Weverything -O3 landmine.c
  • /usr/bin/ld: /tmp/lto-llvm-50c0c6.o: in function `main&apos;:
  • ld-temp.o:(.text.main+0x2): undefined reference to `undefined_function&apos;
  • clang: <font color="#FF5555"><b>error: </b></font><b>linker command failed with exit code 1 (use -v to see invocation)</b></pre>
  • It would be nicer to have a compile-time landmine, but link time is good enough.
#2: Post edited by user avatar alx‭ · 2022-06-23T13:34:03Z (almost 2 years ago)
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #define CONSTANT 0
  • void undefined_function(void);
  • static void foo(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo();
  • else
  • foo();
  • }
  • static void
  • foo(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre>$ clang -Weverything landmine.c
  • /usr/bin/ld: /tmp/landmine-e06cf8.o: in function `foo&apos;:
  • landmine.c:(.text+0x15): undefined reference to `undefined_function&apos;
  • clang: <font color="#FF5555"><b>error: </b></font><b>linker command failed with exit code 1 (use -v to see invocation)</b>
  • </pre>
  • <pre>$ cc -Wall -Wextra landmine.c
  • /usr/bin/ld: /tmp/ccfoyrdJ.o: in function `foo&apos;:
  • landmine.c:(.text+0x15): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status</pre>
  • It would be nicer to have a compile-time landmine, but link time is good enough.
  • Calling an undefined function will have that behavior at link time:
  • `landmine.c`:
  • ```c
  • #define CONSTANT 0
  • void undefined_function(void);
  • static void foo0(void);
  • static void foo1(void);
  • int
  • main(void)
  • {
  • if (CONSTANT)
  • foo1();
  • else
  • foo0();
  • }
  • static void
  • foo0(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • static void
  • foo1(void)
  • {
  • if (!CONSTANT)
  • undefined_function();
  • }
  • ```
  • <pre>$ cc -Wall -Wextra -O3 landmine.c
  • /usr/bin/ld: /tmp/ccH3ypC6.o: in function `main&apos;:
  • landmine.c:(.text.startup+0x5): undefined reference to `undefined_function&apos;
  • collect2: error: ld returned 1 exit status
  • </pre>
  • $ clang -flto -Weverything -O3 landmine.c
  • /usr/bin/ld: /tmp/lto-llvm-50c0c6.o: in function `main':
  • ld-temp.o:(.text.main+0x2): undefined reference to `undefined_function'
  • clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • It would be nicer to have a compile-time landmine, but link time is good enough.
#1: Initial revision by user avatar alx‭ · 2022-06-23T13:31:13Z (almost 2 years ago)
Calling an undefined function will have that behavior at link time:

`landmine.c`:
```c
#define CONSTANT  0


void undefined_function(void);
static void foo(void);


int
main(void)
{
	if (CONSTANT)
		foo();
	else
		foo();
}


static void
foo(void)
{
	if (!CONSTANT)
		undefined_function();
}
```

<pre>$ clang -Weverything landmine.c 
/usr/bin/ld: /tmp/landmine-e06cf8.o: in function `foo&apos;:
landmine.c:(.text+0x15): undefined reference to `undefined_function&apos;
clang: <font color="#FF5555"><b>error: </b></font><b>linker command failed with exit code 1 (use -v to see invocation)</b>
</pre>

<pre>$ cc -Wall -Wextra landmine.c 
/usr/bin/ld: /tmp/ccfoyrdJ.o: in function `foo&apos;:
landmine.c:(.text+0x15): undefined reference to `undefined_function&apos;
collect2: error: ld returned 1 exit status</pre>

It would be nicer to have a compile-time landmine, but link time is good enough.