Obfuscate a C++ program
Requires Codefend 3.3.0 or later for the recommended m_ preset.
Codefend can rename selected identifiers in C++ source before you compile or distribute it. This example totals three values through a small private helper, preserving main and standard library names.
1. Create the source
Install Codefend with Node.js 18 or later and a C++ compiler. These commands use GCC's g++ driver:
npm install --global codefendCreate source/main.cpp:
#include <stdio.h>
static int m_total(int m_first, int m_second, int m_third) {
const int m_sum = m_first + m_second + m_third;
return m_sum;
}
int main() {
printf("%d\n", m_total(12, 18, 12));
return 0;
}Expected obfuscated source
With the configuration in the next step, Codefend produces:
#include <stdio.h>
static int Ox0(int Ox1, int Ox2, int Ox3) {
const int Ox4 = Ox1 + Ox2 + Ox3;
return Ox4;
}
int main() {
printf("%d\n", Ox0(12, 18, 12));
return 0;
}The program's expected output remains 42. Generated identifier numbers can change when the source or Codefend version changes.
The helper, its parameters, and a local result use m_. The stdio.h header and printf function retain their library names. The three values total 42.
2. Configure application names
Create .codefendrc.json in the project root:
{
"generation": {
"inputDir": "source",
"outputDir": "release"
},
"parser": {
"name": "startWithMAndUnderscore"
}
}The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.
It does not parse templates or infer C++ access control. The plain main.cpp filename does not match the rule. The default empty pool avoids limited dictionary allocation across calls.
Keep release as disposable sibling output because Codefend removes that directory before writing. Keep compiled objects, archives, and executables outside the source tree. Read output-directory guidance before running it on an existing project.
3. Compile and compare
Run from the project root:
g++ -std=c++17 source/main.cpp -o original
./original
codefend --obfuscate
g++ -std=c++17 release/main.cpp -o generated
./generatedIn Windows PowerShell, run .\original.exe and .\generated.exe if your compiler adds the executable extension. Both should print:
42Inspect the generated source and test the resulting executable. Use the g++ driver when linking C++ programs; it includes the C++ library automatically. See GCC's C++ compiler documentation.
Preserve external contracts
Public class/member names, exported symbols, plugin interfaces, reflection registrations, and serialized field names can be consumed outside the files you transform. Keep those names stable unless every consumer is coordinated.
Templates, macros, generated headers, resource files, and build-system rules can carry additional references. Codefend does not update an ABI or understand overloads and C++ scopes. Extend the naming convention gradually and compile the complete generated tree, including tests.
Matching text in strings and comments can also change. Review current limitations, especially name collisions and shared pools, before extending this example.
For Arduino-specific filename and callback requirements, see the Arduino example.
Add Codefend to your workflow
Run Codefend before your existing C++ build and compile the generated source tree. Keep compiler settings, dependencies, and binary outputs in the build system; run the generated program and project tests before packaging it.
codefend --obfuscate
g++ -std=c++17 release/main.cpp -o generated
./generatedIn CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.