Generating assembly output from msvc and CMake

Sometimes it’s needed or you may require to see the assembly output listing of a C++ snippet code just for the testing purpose. Testing yourself with Jason Turner’s session might be the example.

If you’re using a msvc with Windows environment, then it’s quite easy by running the simple command line argument:

Let’s say we have an optimizaing.cpp as following:

#include <string>

int main()

{

    std::string s(“a”);

    return s.size();

}

Launch the build environment and run the cl.exe with a ‘/FA’ parameter.


cl /EHsc /nologo /W4  optimizing.cpp /FAs /GL /O2


CMake also automatically adds the target generating assembly output and it’ll be also a good choice when you’re not using msvc.

Let’s say if we have the following CMakeLists.txt,

project(demo)

cmake_minimum_required(VERSION 3.1)

#—————————————————-

# Flags

#—————————————————-

set(CMAKE_CXX_STANDARD 14)

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=1)

#—————————————————-

# Executable

#—————————————————-

add_executable(optimizing “optimizing.cpp”)


You can generate the build files. I use theMinGWw g++ toolchain here.

cmake -G “MinGW Makefiles” -DCMAKE_BUILD_TYPE=RELEASE ..


Then you’ll see the target.s for the assembly output. For example, the generated Makefile contains ‘optimizing.s’ here.


You’ll finally get the assembly output listing by running make target.s as below:


 

Thanks,

Heejune