This is a tiny script that is enough to get gcc to fail when trying to build:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <htslib/sam.h>
    
    int main(int argc, char *argv[]){
    
        samFile *fp_in = hts_open(argv[1],"r"); //open bam file
        bam_hdr_t *bamHdr = sam_hdr_read(fp_in); //read header
        bam1_t *aln = bam_init1(); //initialize an alignment
    }

This is my minimal command for trying to build:

    gcc test.c /mnt/work/me/software/anaconda/pkgs/htslib-1.9-hc238db4_4/lib/libhts.a  -I /mnt/work/me/software/anaconda/pkgs/htslib-1.9-hc238db4_4/include/

What happens is:

    /mnt/work/me/software/anaconda/pkgs/htslib-1.9-hc238db4_4/lib/libhts.a(hts.o): In function `decompress_peek':
    hts.c:(.text+0x14a5): undefined reference to `inflateInit2_'
    hts.c:(.text+0x14c6): undefined reference to `inflate'
    hts.c:(.text+0x14f9): undefined reference to `inflateEnd'
    /mnt/work/me/software/anaconda/pkgs/htslib-1.9-hc238db4_4/lib/libhts.a(cram_io.o): In function `itf8_decode_crc':
    cram_io.c:(.text+0x18e0): undefined reference to `libdeflate_crc32'
    cram_io.c:(.text+0x1938): undefined reference to `libdeflate_crc32'
    cram_io.c:(.text+0x19b6): undefined reference to `libdeflate_crc32'
    cram_io.c:(.text+0x1a5a): undefined reference to `libdeflate_crc32'
    cram_io.c:(.text+0x1b2d): undefined reference to `libdeflate_crc32'

1) What am I missing to get this script to compile? 

2) And how do I find out where htslib is installed on the end users system? I can am using setup.py to install the script so I am free to run arbitrary code to find it.

You are moving from building existing packaged tools, which is (admittedly less than ideally) something that many bioinformaticians need to do when they'd really like to be just using the tools, to writing the build infrastructure from scratch for your own tools, which is a software development task and in a different category.

So the HTSlib etc maintainers go to great lengths to make building HTSlib, SAMtools, etc Just Work, but there is less pressure to provide full instructions and Just-Workingness for people writing their own tools that use HTSlib, as there is a greater expectation that such people know what they're doing.

To get this **program** to compile, you are missing `-lz` from your link command. And something similar for the other errors. Unfortunately one of the skills you are going to have to learn in order to write a tool like this is the skill of debugging build issues like this one. There aren't many shortcuts other than experience, and help and advice from a more experienced programmer or sysadmin at your institute who can look over your shoulder at your screen.

The way to gain that experience starts with googling those error messages to figure out what you need to add to get this to compile and link. And learning about the different stages (compiling, linking, installing) involved in building a C program.