This template demonstrates how to use fortplot in a CMake-based Fortran project. It provides a complete working example with modern CMake practices and comprehensive plotting demonstrations.
-
Copy this template to your project directory:
cp -r cmake_project_template my_cmake_project cd my_cmake_project -
Build the project:
cmake -B build cmake --build build
-
Run the demo:
./build/bin/fortplot_demo
- Automatic dependency fetching using
FetchContent - Proper target linking with
fortplot::fortplot - Compiler flag management for debug/release builds
- Fortran standard compliance (Fortran 2008+)
The template program shows:
- Basic plots - Mathematical functions with legends and styling
- Scientific visualization - Error bars, subplots, and data comparison
- Multiple output formats - PNG, PDF, and ASCII output
cmake_project_template/
├── CMakeLists.txt # CMake configuration with fortplot integration
├── src/
│ └── main.f90 # Demonstration program with multiple examples
├── build/ # Build directory (created by CMake)
└── README.md # This file
-
Edit project name in
CMakeLists.txt:project(YourProjectName VERSION 1.0.0 LANGUAGES Fortran)
-
Replace the demo program in
src/main.f90:program your_analysis use fortplot implicit none ! Your plotting code here call figure() ! ... create your plots ... call savefig("your_results.png") end program your_analysis
-
Update executable name:
add_executable(your_program src/main.f90 )
For larger projects with multiple source files:
# Multiple source files
add_executable(your_program
src/main.f90
src/data_analysis.f90
src/plotting_utils.f90
)
# Add include directories if needed
target_include_directories(your_program PRIVATE src)# Add other libraries
find_package(BLAS REQUIRED)
target_link_libraries(your_program
PRIVATE
fortplot::fortplot
${BLAS_LIBRARIES}
)cmake -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build- Enables debugging symbols
- Adds runtime checks
- Includes compiler warnings
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build- Optimizes for performance
- Removes debugging overhead
- Production-ready code
cmake -B build -DCMAKE_Fortran_FLAGS="-march=native -O3"
cmake --build buildThe template includes comprehensive tests that verify:
- Compilation - CMake finds and links fortplot correctly
- Basic plotting - Simple line plots with styling
- Advanced features - Error bars, subplots, multiple formats
- Output generation - PNG, PDF, and ASCII file creation
If the template builds and runs without errors, your CMake + fortplot setup is working perfectly.
Problem: CMake can't find fortplot
Could not resolve fortplot dependency
Solution: Check internet connection and git access:
git ls-remote https://github.com/lazy-fortran/fortplotProblem: Compiler not found
No CMAKE_Fortran_COMPILER could be found
Solution: Install gfortran:
# Ubuntu/Debian
sudo apt install gfortran
# macOS
brew install gcc
# Windows (MSYS2)
pacman -S mingw-w64-x86_64-gcc-fortranProblem: Fortran standard errors Solution: Ensure gfortran version 8+ or adjust standard:
set(CMAKE_Fortran_STANDARD 2003) # For older compilersProblem: Linking errors Solution: Check that fortplot target is properly linked:
target_link_libraries(your_program PRIVATE fortplot::fortplot)Problem: Output files not created Solution: Check write permissions and disk space:
ls -la *.png *.pdf *.txtProblem: Segmentation fault Solution: Build with debug flags:
cmake -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build
gdb ./build/bin/fortplot_demo- Use Release builds for production code
- Enable native optimization for your CPU
- Profile your code with gprof if needed
- Consider OpenMP for parallel computations
- Explore the documentation:
/doc/user_compilation_guide.md - Study more examples:
/example/fortran/*/ - Read the API documentation:
make docin the main project - Join the community: Contribute to the fortplot project
Happy plotting with CMake!