Search code examples
c++cmakecompilationlinkerlinker-flags

Difference between add_compile_options and add_link_options also flags each option supports


I've been using a .bat file to build my applications for years. Recently, switched to CMake for it's elegancy and I ran into a situation where I had to guess the flags that I should put into CMake's add_link_options and add_compile_options

Example flags that I've used in my BAT file,

-s WASM=1 --bind -s MODULARIZE=1

And, In CMake this flags have become (After trial and error),

add_compile_options("SHELL: -s WASM=1")
add_link_options("SHELL: --bind")
add_link_options("SHELL: -s MODULARIZE=1")

Honestly, I can't find any information regards flags that add_link_options and add_compile_options supports.

I know what is a linker is but lost when it comes to add_link_options or linker flags.

I'm used to compile everything in single line and now in CMake everything appear to be involve separate steps.


Solution

  • I am not sure what your problem is, but here is a full working sample from a Wasm project that sets project-wide strict mode and disabling of exception support:

    if (EMSCRIPTEN)
        add_compile_options(-fno-exceptions "SHELL:-s STRICT=1")
        add_link_options("SHELL:-s STRICT=1")
    endif()
    

    Note in particular that, as it has a [compile+link] marker in the emscripten settings, -s STRICT=1 has to be used both for compiling and for linking, thus it appears in each.

    The if(EMSCRIPTEN) around is there because this project can also be built for Windows and Linux.