This guide shows you how to build the infix library and add it to your C projects.
You will need a C17-compatible compiler:
- GCC 8+
- Clang 7+
- Visual Studio 2019+
For convenience, a build system is recommended:
The simplest way to use infix is to compile it directly into your project. This "unity build" approach requires no separate build steps and allows for better compiler optimizations.
- Copy the
src/andinclude/directories from theinfixrepository into your project (e.g., into athird_party/infixsubdirectory). - Add
third_party/infix/src/infix.cto your list of source files to be compiled. - Add the path to the
includedirectory to your compiler's include search paths (e.g.,-Ithird_party/infix/include).
That's it. Your project will now build with the
infixlibrary compiled directly in.
If you prefer to build infix as a separate static library (.a or .lib) for system-wide installation or use with tools like pkg-config, follow the instructions for your preferred build system.
I personally use the perl based build script to develop, test, and fuzz infix. From the root of the infix project:
# Build the library
perl build.pl
# Run tests
perl build.pl testxmake provides an equally simple, cross-platform build experience. From the root of the infix project:
# Build the library
xmake
# Run tests
xmake testCMake can generate native build files for your environment (e.g., Makefiles, Visual Studio solutions).
# Configure the project in a 'build' directory
cmake -B build
# Compile the library
cmake --build build
# Run tests
ctest --test-dir build
# Install the library and headers (may require sudo)
cmake --install buildTo install to a custom location, use -DCMAKE_INSTALL_PREFIX=/path/to/install during the configure step.
GNU Make (Linux, macOS):
make
make test
sudo make installNMake (Windows with MSVC): Open a Developer Command Prompt and run:
nmake /f Makefile.winWhen building infix as a shared library (DLL/.so), you must ensure that only the public API is exported.
Windows (DLL):
- Building: You must define
INFIX_BUILDING_DLLwhen compiling the library. This ensures that__declspec(dllexport)is applied only to public API functions, preventing internal symbols from polluting the DLL export table. - Using: You must define
INFIX_USING_DLLwhen compiling code that links against theinfixDLL. This applies__declspec(dllimport)to the function declarations.
Linux/macOS:
All public functions are marked with INFIX_API (default visibility). Internal functions are marked with INFIX_INTERNAL (hidden visibility). The provided build scripts automatically set -fvisibility=hidden to ensure INFIX_INTERNAL works as intended.
Static Library:
Do not define INFIX_BUILDING_DLL or INFIX_USING_DLL. This is the default.
The provided build scripts (perl, xmake, CMake, Makefiles) handle these definitions automatically when you select a shared library build.
For minimal environments or direct embedding, you can compile the library manually. The Perl-based builder is also available for advanced development tasks. For details, see the original INSTALL.md.
Once infix is built and installed, you can link it into your application.
If you installed infix to a standard or custom location, you can use find_package in your CMakeLists.txt:
# Find the installed infix library.
# If installed to a custom path, run cmake with:
# cmake -DCMAKE_PREFIX_PATH=/path/to/your/install ..
find_package(infix REQUIRED)
# Link your application against the imported target.
target_link_libraries(my_app PRIVATE infix::infix)A infix.pc file is generated for use with pkg-config, which is useful in Makefiles or autotools projects.
CFLAGS = `pkg-config --cflags infix`
LIBS = `pkg-config --libs infix`
my_app: main.c
$(CC) -o my_app main.c $(CFLAGS) $(LIBS)If you are using xmake for your project, you can add infix as a dependency in your xmake.lua.
-- Tell xmake where to find the infix project
add_requires("infix", {git = "https://github.com/sanko/infix.git"})
-- Link your application against the "infix" library
target("my_app")
set_kind("binary")
add_files("src/*.c")
add_deps("infix")If you've added the infix source to a subdirectory (e.g., libs/infix), you can configure VS Code's IntelliSense and build tasks.
.vscode/c_cpp_properties.json (for IntelliSense)
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/libs/infix/include" // <-- Path to infix headers
],
"cStandard": "c17"
}
]
}.vscode/tasks.json (for Building)
{
"version": "2.0.0",
"tasks": [
{
"label": "build with infix",
"type": "shell",
"command": "gcc",
"args": [
"-std=c17", "-g", "${file}",
"-I${workspaceFolder}/libs/infix/include", // Find infix headers
"-L${workspaceFolder}/libs/infix/build", // Find libinfix.a
"-linfix", // Link the library
"-o", "${fileDirname}/${fileBasenameNoExtension}"
]
}
]
}