Run Your First C++ File
This page shows how to run a single C++ file with Vix.
The main command is:
vix run main.cppThis is the fastest way to start using Vix.
Create a workspace
Create a temporary folder:
mkdir -p ~/tmp/vix-first-file
cd ~/tmp/vix-first-fileCreate main.cpp:
touch main.cppWrite your first C++ program
Open main.cpp and write:
#include <iostream>
int main()
{
std::cout << "Hello from Vix\n";
return 0;
}Run it:
vix run main.cppExpected output:
Hello from VixWhat happened?
When you run:
vix run main.cppVix detects that you passed a single .cpp file.
It then:
detects the file → compiles it → runs it → prints the outputYou do not need to create a full project yet.
This mode is useful for:
- quick experiments
- learning Vix
- testing small ideas
- writing small C++ tools
- running examples
Run a Vix HTTP file
Now replace main.cpp with a small Vix HTTP app:
#include <vix.hpp>
using namespace vix;
int main()
{
App app;
app.get("/", [](Request &, Response &res) {
res.text("Hello Vix");
});
app.run(8080);
return 0;
}Run it:
vix run main.cppExpected output shape:
Vix.cpp READY
HTTP: http://localhost:8080/
Status: readyOpen another terminal and test the server:
curl -i http://127.0.0.1:8080/Expected response:
Hello VixStop the server with:
Ctrl+CReturn JSON
Most Vix backend apps return JSON.
Update the route:
#include <vix.hpp>
using namespace vix;
int main()
{
App app;
app.get("/", [](Request &, Response &res) {
res.json({
"message", "Hello from Vix",
"framework", "Vix.cpp"
});
});
app.get("/health", [](Request &, Response &res) {
res.json({
"ok", true,
"service", "first-file"
});
});
app.run(8080);
return 0;
}Run it again:
vix run main.cppTest it:
curl -i http://127.0.0.1:8080/
curl -i http://127.0.0.1:8080/healthAdd a route parameter
Vix routes can contain path parameters.
Add this route before app.run(8080):
app.get("/hello/{name}", [](Request &req, Response &res) {
const std::string name = req.param("name");
res.json({
"greeting", "Hello " + name,
"powered_by", "Vix.cpp"
});
});Run:
vix run main.cppTest:
curl -i http://127.0.0.1:8080/hello/GaspardExpected response shape:
{
"greeting": "Hello Gaspard",
"powered_by": "Vix.cpp"
}Add a query parameter
Query parameters come after ? in the URL.
Add this route before app.run(8080):
app.get("/users/{id}", [](Request &req, Response &res) {
const std::string id = req.param("id");
const std::string page = req.query_value("page", "1");
res.json({
"id", id,
"page", page
});
});Run:
vix run main.cppTest:
curl -i "http://127.0.0.1:8080/users/42?page=2"Expected response shape:
{
"id": "42",
"page": "2"
}Complete example
Your full main.cpp can look like this:
#include <vix.hpp>
using namespace vix;
int main()
{
App app;
app.get("/", [](Request &, Response &res) {
res.json({
"message", "Hello from Vix",
"framework", "Vix.cpp"
});
});
app.get("/health", [](Request &, Response &res) {
res.json({
"ok", true,
"service", "first-file"
});
});
app.get("/hello/{name}", [](Request &req, Response &res) {
const std::string name = req.param("name");
res.json({
"greeting", "Hello " + name,
"powered_by", "Vix.cpp"
});
});
app.get("/users/{id}", [](Request &req, Response &res) {
const std::string id = req.param("id");
const std::string page = req.query_value("page", "1");
res.json({
"id", id,
"page", page
});
});
app.run(8080);
return 0;
}Run:
vix run main.cppTest:
curl -i http://127.0.0.1:8080/
curl -i http://127.0.0.1:8080/health
curl -i http://127.0.0.1:8080/hello/Gaspard
curl -i "http://127.0.0.1:8080/users/42?page=2"Pass runtime arguments
Runtime arguments are arguments passed to your program.
Use --run:
vix run main.cpp --run --port 8080Use this when your program reads argv.
Example:
#include <iostream>
int main(int argc, char **argv)
{
std::cout << "argc = " << argc << "\n";
for (int i = 0; i < argc; ++i)
{
std::cout << "argv[" << i << "] = " << argv[i] << "\n";
}
return 0;
}Run:
vix run main.cpp --run --port 8080Important: --run vs --
In script mode, --run is for runtime arguments.
vix run main.cpp --run --port 8080The -- separator is for compiler or linker flags.
vix run main.cpp -- -O2 -DNDEBUGDo not use -- for runtime arguments in script mode.
Wrong:
vix run main.cpp -- --port 8080Correct:
vix run main.cpp --run --port 8080Pass compiler flags
Use -- to pass compiler or linker flags:
vix run main.cpp -- -O2 -DNDEBUGLink with libraries:
vix run main.cpp -- -lssl -lcryptoAdd include paths:
vix run main.cpp -- -I./includeUse watch mode
During development, you can rebuild and restart when the file changes:
vix run main.cpp --watchFor project development, you will usually use:
vix devYou will learn this in the next pages.
Use sanitizers
For debugging memory issues:
vix run main.cpp --sanFor undefined behavior checks:
vix run main.cpp --ubsanCommon mistakes
Using CLI-only installation
If your code uses:
#include <vix.hpp>you need the full SDK.
Check:
find ~/.local/include -name vix.hpp 2>/dev/nullIf nothing appears, reinstall Vix:
curl -fsSL https://vixcpp.com/install.sh | bashPassing runtime args after --
Wrong:
vix run main.cpp -- --port 8080Correct:
vix run main.cpp --run --port 8080Port 8080 is already in use
If the server cannot start, another process may already be using port 8080.
Check:
sudo lsof -i :8080Stop the other process or change the port:
app.run(3000);Running from the wrong directory
Relative paths depend on the directory where you run vix.
For example:
res.file("public/index.html");will look for:
public/index.htmlrelative to your current working directory.
When to create a project
A single file is perfect for learning.
Move to a project when you need:
- multiple
.cppfiles - headers
- tests
- dependencies
.envconfiguration- a stable folder structure
- release builds
The next page shows how to create a real Vix project.
What you should remember
The main command is:
vix run main.cppUse:
--runfor runtime arguments--for compiler and linker flags
Single-file mode is the fastest way to start.
When your app grows, create a project.
Next step
Create your first Vix project.