Zig cc cross compile problem

Hello everyone,

I am quite a newbie to all of these things, so it helps a lot if someone points out something obviously wrong.

I want to compile the code from here
which is

#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>

int main(int argc, char **argv) 
  unsigned int packet_counter=0;
  struct pcap_pkthdr header; 
  const u_char *packet;

  if (argc < 2) { 
    fprintf(stderr, "Usage: %s <pcap>\n", argv[0]); 

   pcap_t *handle; 
   char errbuf[PCAP_ERRBUF_SIZE];  
   handle = pcap_open_offline(argv[1], errbuf); 

   if (handle == NULL) { 
     fprintf(stderr,"Couldn't open pcap file %s: %s\n", argv[1], errbuf); 

   while (packet = pcap_next(handle,&header)) { 



  printf("%d\n", packet_counter);
  return 0;

It is fine to compile with gcc -o count count.c -lpcap -lc and zig cc -lpcap -lc count.c -o count

But when it comes to zig cc -lpcap -lc -target x86_64-linux count.c -o count,
it will be
count.c:2:10: fatal error: 'pcap.h' file not found

I use 0.8.1.
Is there any suggestion?
I want to make it not a dynamic executable.

When you compile for your native target (without -target ...), the compiler will look through your system headers. This post looks like it might explain how that works fairly well: Library path in gcc

When you change the target, those native libraries will no longer work so the compiler uses different search paths. There might be conventions about where to install non-native headers/libraries, this will probably depend on your linux distribution. Or you can compile the library yourself for the non-native target and add the path to the location you built it with (-I for header directorys and -L for library directories, which is where the .so files are). So if you want to learn more and make progress, I would suggest download the pcap source code and seeing if you could compile it for the non-native target as well, then add the appropriate -I header_dir -L lib_dir arguments to your zig cc command for count.c.

After adding -I header_dir -L lib_dir (clone from github), I can build it now. Thanks a lot!

But it seems to be a dynamic linking, and
zig build-exe -static -lpcap -lc -target x86_64-linux-gnu -I header_dir -L lib_dir count.c
results in error: unable to create compilation: UnableToStaticLink.

Build from source will make a lot of ld.lld: error: undefined symbol.

According to Compilation.zig, it might not be possible to create a static executable for now.

Anyway, thanks for helping!

gnu libc can’t be statically linked afaik, if you want to create a static executable try using x86_64-linux-musl as the target

1 Like