Skip to content

Instantly share code, notes, and snippets.

@scivision

scivision/CMakeLists.txt

Last active Feb 26, 2021
Embed
What would you like to do?
GCC static exe linking
cmake_minimum_required(VERSION 3.14)
project(demo Fortran)
# --- static flags avoid users needing libgfortran etc. on their Windows system
# MacOS and Linux needs more caution as true static linking is an advanced topic.
# this does not guarantee a portable executable, careful testing is needed and possibly further options
set(static_link_flag)
if(MINGW AND CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
set(static_link_flags -static -static-libgfortran)
# MinGW / MSYS2 special considerations: https://www.msys2.org/news/#2021-01-31-aslr-enabled-by-default
list(APPEND static_link_flags -Wl,--default-image-base-low)
endif()
# example
add_exectuable(main main.f90)
target_link_options(main PRIVATE ${static_link_flags})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment