Please read the new rule regarding the restriction on the use of AI tools. ×

Ways of compling

Revision en2, by yshen94, 2024-10-11 12:11:12
  1. Use a Makefile (Recommended) A Makefile allows you to automate your build process and avoid typing the same commands every time. Here's how you can set it up for your project:

Step 1: Create a Makefile Create a file named Makefile in your project directory with the following content:

makefile Copy code

Define your compiler

CXX = g++

Define your paths and flags

INCLUDE_PATH = -I/opt/homebrew/Cellar/raylib/5.0/include LIB_PATH = -L/opt/homebrew/Cellar/raylib/5.0/lib LIBS = -lraylib

Define your source files and output binary

SRC = main.cpp OUT = my_program

Compilation rule

all: $$$(CXX) $$$(SRC) -o $$$(OUT) $$$(INCLUDE_PATH) $$$(LIB_PATH) $$$(LIBS) Step 2: Build Using the Makefile Now, instead of running the long g++ command, you can just type:

bash Copy code make This will execute the commands specified in the Makefile and build your project.

  1. Configure VS Code Tasks If you're using Visual Studio Code, you can configure a task that automates the build process.

Step 1: Create or Update tasks.json In your .vscode folder, create or edit the tasks.json file to include your build command:

json Copy code { "version": "2.0.0", "tasks": [ { "label": "Build Tetris Game", "type": "shell", "command": "g++", "args": [ "-o", "my_program", "main.cpp", "-I/opt/homebrew/Cellar/raylib/5.0/include", "-L/opt/homebrew/Cellar/raylib/5.0/lib", "-lraylib" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Custom build task for Tetris game with raylib" } ] } Step 2: Build Using the Task Now, in VS Code, press Cmd+Shift+B (or go to Terminal > Run Build Task) to run the task and compile your project. This saves you from typing the g++ command every time.

  1. Use Environment Variables You can define environment variables for the include and library paths to make the command shorter.

Step 1: Set Environment Variables in Shell Profile Add the following lines to your .zshrc (if you're using zsh) or .bashrc (for bash):

bash Copy code export CXXFLAGS="-I/opt/homebrew/Cellar/raylib/5.0/include" export LDFLAGS="-L/opt/homebrew/Cellar/raylib/5.0/lib -lraylib" After editing the file, reload the shell configuration:

bash Copy code source ~/.zshrc # or ~/.bashrc for bash Step 2: Compile Using Environment Variables Now, you can compile your program using the simplified command:

bash Copy code g++ -o my_program main.cpp $CXXFLAGS $LDFLAGS This approach lets you keep your compile commands shorter but still pass the correct paths and libraries.

  1. Use pkg-config (for Libraries that Support It) If a library provides pkg-config support, you can avoid manually specifying -I and -L flags by using pkg-config. However, this only works if the library is installed in a way that pkg-config can detect.

For raylib, if it supports pkg-config, you can run:

bash Copy code g++ -o my_program main.cpp $(pkg-config --libs --cflags raylib) This will automatically include the correct -I and -L flags for raylib.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English yshen94 2024-10-11 12:27:32 328 (published)
en4 English yshen94 2024-10-11 12:19:04 3007 (saved to drafts)
en3 English yshen94 2024-10-11 12:11:29 1
en2 English yshen94 2024-10-11 12:11:12 16
en1 English yshen94 2024-10-11 12:10:03 3335 Initial revision (published)