# Compiling C code failed

**URL:** <https://forum.openwrt.org/t/compiling-c-code-failed/29546>\
**Category:** For Developers\
**Created:** [January 20, 2019, 4:52pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546 "2019-01-20T16:52:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![peregr1nefalco](https://forum.openwrt.org/letter_avatar/peregr1nefalco/32/5_5575768a8748004e209b776fc1b2916d.png) [@peregr1nefalco](https://forum.openwrt.org/u/peregr1nefalco)\
**Post date:** [January 20, 2019, 4:52pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/1 "2019-01-20T16:52:58Z")

</div>

Hi, newbie here. I'm trying to compile [notsodeep](https://github.com/farukuzun/notsodeep), which is an active deep packet inspection circumvention utility for Linux. It works well in my testing (with Debian x86), but since there are no binaries for openwrt I decided to compile it. However, I ran into the following issue:

```
rickz@rickz-deb:~/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/bin$ ./mips-openwrt-linux-musl-gcc-7.4.0 -Wall -Wextra -Wformat-security -O3 -fstack-protector-all -lnetfilter-queue -lnfnetlink notsodeep.c -o notsodeep
mips-openwrt-linux-musl-gcc-7.4.0: warning: environment variable 'STAGING_DIR' not defined
notsodeep.c: In function 'tcp_synack_segment':
notsodeep.c:13:12: error: 'struct tcphdr' has no member named 'urg'
  if (tcphdr->urg == 0 &&
            ^~
notsodeep.c:14:11: error: 'struct tcphdr' has no member named 'ack'; did you mean 'th_ack'?
   tcphdr->ack == 1 &&
           ^~~
           th_ack
notsodeep.c:15:9: error: 'struct tcphdr' has no member named 'psh'
   tcphdr->psh == 0 &&
         ^~
notsodeep.c:16:9: error: 'struct tcphdr' has no member named 'rst'
   tcphdr->rst == 0 &&
         ^~
notsodeep.c:17:9: error: 'struct tcphdr' has no member named 'syn'
   tcphdr->syn == 1 &&
         ^~
notsodeep.c:18:9: error: 'struct tcphdr' has no member named 'fin'
   tcphdr->fin == 0) {
         ^~
notsodeep.c: In function 'tcp_checksum':
notsodeep.c:35:14: error: 'struct tcphdr' has no member named 'check'; did you mean 'th_ack'?
     tcphdrp->check = 0;
              ^ ~~~~
              th_ack
notsodeep.c:48:14: error: 'struct tcphdr' has no member named 'check'; did you mean 'th_ack'?
     tcphdrp->check = (unsigned short)sum;
              ^ ~~~~
              th_ack
notsodeep.c: In function 'rewrite_win_size':
notsodeep.c:58:8: error: 'struct tcphdr' has no member named 'window'
  tcphdr->window = htons(new_window);
        ^~
>

```

From what I read, it is because of different C libraries. Is that so? Is there anything I could do to compile it properly? And I tried to set the staging\_dir, but it results in:

```
rickz@rickz-deb:~/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/bin$ export STAGING_DIR=/home/rickz/openwrt/staging_dir
rickz@rickz-deb:~/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/bin$ ./mips-openwrt-linux-musl-gcc-7.4.0 -Wall -Wextra -Wformat-security -O3 -fstack-protector-all -lnetfilter-queue -lnfnetlink notsodeep.c -o notsodeep
notsodeep.c:8:10: fatal error: libnetfilter_queue/libnetfilter_queue.h: No such file or directory
 #include <libnetfilter_queue/libnetfilter_queue.h>
          ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

```

Any pointers? Thanks before, much appreciated.

---

<div class="post-metadata">

**Author:** ![hnyman](https://forum.openwrt.org/user_avatar/forum.openwrt.org/hnyman/32/19_2.png) [@hnyman](https://forum.openwrt.org/u/hnyman)\
**Post date:** [January 20, 2019, 5:08pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/2 "2019-01-20T17:08:21Z")

</div>

You should craft a proper Openwrt Makefile for the package. That would set the needed options for compiler.

---

<div class="post-metadata">

**Author:** ![peregr1nefalco](https://forum.openwrt.org/letter_avatar/peregr1nefalco/32/5_5575768a8748004e209b776fc1b2916d.png) [@peregr1nefalco](https://forum.openwrt.org/u/peregr1nefalco)\
**Post date:** [January 20, 2019, 5:13pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/3 "2019-01-20T17:13:23Z")

</div>

Thank you. I have read [the wiki](https://openwrt.org/docs/guide-developer/packages#bundle_source_code_with_openwrt_makefile), but I am completely unfamiliar with the options (and the build system in general), also documentation on this program is scarce. Can I have a dumbed-down example of a makefile?

---

<div class="post-metadata">

**Author:** ![jan](https://forum.openwrt.org/user_avatar/forum.openwrt.org/jan/32/135636_2.png) [@jan](https://forum.openwrt.org/u/jan)\
**Post date:** [January 20, 2019, 10:10pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/4 "2019-01-20T22:10:20Z")

</div>

In my case I set STAGING\_DIR to something like `myopenwrt/staging_dir/target-arm_cortex-a8+vfpv3_musl_eabi`, you must add the last directory, of course adapted to your target.

---

<div class="post-metadata">

**Author:** ![jeff](https://forum.openwrt.org/letter_avatar/jeff/32/5_5575768a8748004e209b776fc1b2916d.png) [@jeff](https://forum.openwrt.org/u/jeff)\
**Post date:** [January 20, 2019, 10:18pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/5 "2019-01-20T22:18:15Z")

</div>

Some references I have found useful include:

> [@Packaging guidelines](https://forum.openwrt.org/t/packaging-guidelines/65/2):
>
> Moved to [https://openwrt.org/docs/guide-developer/package-policies](https://openwrt.org/docs/guide-developer/package-policies)

> **[Creating packages](https://openwrt.org/docs/guide-developer/packages)**
>
> Creating packages See also -\> Package Policy Guide, which contains a wealth of extra technical information not covered here. One of the things that we've attempted to do with OpenWrt's template system is make it incredibly easy to port software...

> **[Building a single package](https://openwrt.org/docs/guide-developer/toolchain/single.package)**
>
> Building a single package Useful if you want to upgrade a package without reflashing the router. Follow the Build system usage up to the point when you make menuconfig. In here, select the target platform, then tick the package you want to build,...

Not knowing what the "usual" build process is for "notsodeep", I can't directly recommend an example Makefile, but there are many examples in `./package/` and `./feeds/`

---

<div class="post-metadata">

**Author:** ![peregr1nefalco](https://forum.openwrt.org/letter_avatar/peregr1nefalco/32/5_5575768a8748004e209b776fc1b2916d.png) [@peregr1nefalco](https://forum.openwrt.org/u/peregr1nefalco)\
**Post date:** [January 20, 2019, 11:43pm UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/6 "2019-01-20T23:43:12Z")

</div>

@jan I have set the staging\_dir to the toolchain directory, same result. Thanks anyway!

@jeff

> Not knowing what the "usual" build process is for "notsodeep", I can't directly recommend an example Makefile, but there are many examples in `./package/` and `./feeds/`

If its any help, this is the makefile that comes with notsodeep. I tried to find settings regarding compiling in Makefiles in `./package`, but to no avail.

```plaintext
CC ?= gcc
CFLAGS = -Wall -Wextra -Wformat-security -O3 -fstack-protector-all
LIBS = -lnetfilter_queue -lnfnetlink
PROGRAM = notsodeep
SOURCE = notsodeep.c

all: notsodeep 

notsodeep: notsodeep.c
	$(CC) $(SOURCE) $(CFLAGS) $(LIBS) -o $(PROGRAM)

clean:
	@rm -rf $(PROGRAM)

```

---

<div class="post-metadata">

**Author:** ![jow](https://forum.openwrt.org/user_avatar/forum.openwrt.org/jow/32/13_2.png) [@jow](https://forum.openwrt.org/u/jow)\
**Post date:** [January 21, 2019, 8:36am UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/7 "2019-01-21T08:36:30Z")

</div>

You need to build with `-D_GNU_SOURCE` or even better, with `-std=gnu99` in `TARGET_CFLAGS`. The `struct tcphdr` fields mentioned in your error message are protected by an `#ifdef _GNU_SOURCE` feature test macro in musl libc.

---

<div class="post-metadata">

**Author:** ![peregr1nefalco](https://forum.openwrt.org/letter_avatar/peregr1nefalco/32/5_5575768a8748004e209b776fc1b2916d.png) [@peregr1nefalco](https://forum.openwrt.org/u/peregr1nefalco)\
**Post date:** [January 21, 2019, 10:17am UTC](https://forum.openwrt.org/t/compiling-c-code-failed/29546/8 "2019-01-21T10:17:48Z")

</div>

@jow Yes, I stumbled upon the commented `define _GNU_SOURCE 1` in `include/features.h`. I simply uncommented it, and it looks like the `struct tcphdr` errors have gone. Probably not a good practice, hehe.

However I am now facing a new problem. When attempting to compile, this came out:

```
rickz@rickz-deb:~/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/bin$ ./mips-openwrt-linux-musl-gcc -Wall -Wextra -Wformat-security -O3 -fstack-protector-all -lnetfilter-queue -lnfnetlink notsodeep.c -o notsodeep
mips-openwrt-linux-musl-gcc: warning: environment variable 'STAGING_DIR' not defined
mips-openwrt-linux-musl-gcc: warning: environment variable 'STAGING_DIR' not defined
mips-openwrt-linux-musl-gcc: warning: environment variable 'STAGING_DIR' not defined
/home/rickz/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/lib/gcc/mips-openwrt-linux-musl/7.4.0/../../../../mips-openwrt-linux-musl/bin/ld: cannot find -lnetfilter-queue
/home/rickz/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/lib/gcc/mips-openwrt-linux-musl/7.4.0/../../../../mips-openwrt-linux-musl/bin/ld: cannot find -lnfnetlink
collect2: error: ld returned 1 exit status

```

It seems that the libraries are missing. How can I add them?
