When writing a module in C, how to correctly reference header files upon which the module is built?

237 views Asked by At

For example, when writing an NGINX module and needing to include headers from said module, how do I do it?

#include <ngx_core.h>
#include <ngx_http.h>

Naturally, the headers are not found as they don't exist on disk. I doubt the proper way to solve this is to bring in the entire NGINX source's headers just so I can reference the headers in my module.

I'm looking for design-time compiler feedback here, not looking for compile-time supports since the module is built using NGINX's configure script which handles wiring things up for compilation.

More specifically, how do I resolve this in VS Code without bringing in all the NGINX header files into my src dir? Is there some sort of symbols file I need?

If I need to have a top-level lib dir with the headers, that's fine, but I cannot change the include paths within my source files (so I couldn't change the include paths to e.g. ../lib/nginx/ngx_core.h) because this will fail during compile time.

missing headers

1

There are 1 answers

0
Josh M. On

I was able to resolve this by cloning the NGINX repo (and others I needed) and adding include paths to my VS Code config for my project so the C/C++ extension can find them.

.vscode/c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "~/Projects/third-party/nginx/objs/**",
                "~/Projects/third-party/nginx/src/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++14",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

For NGINX, this requires you to download the release/GZip, extract it where you want, and then run ./configure -- this generates some things/headers in objs which are required.