valettearnaud
Clangd and CMake: webAssembly
(Note: be sure to tell emacs to use the clangd version you installed on your system)
(setq lsp-clangd-binary-path "/usr/bin/clangd")
Emscripten is a complete compiler toolchain to WebAssembly, using LLVM, with a special focus on speed, size, and the Web platform.
If you happen to want programming in C/C++ for the web,
if you happen to live inside of emacs,
you are probably using clangd and emscripten.
As you may know clangd is most useful when a compile_commands.json
database file is generated in your project, you have several options
to generate one, but all in all, you generally have to use a software build
system;
this is how I began using CMake.
At the root of your project, you have to specify the build
process in a CMakeLists.txt file, here we provide a basic
file which tell CMake to:
- require version
3.8 - name the project "someProjectName"
- ensure the compiler uses C++17
- add "test" as an executable, compiled from the single file src/test.cpp
- set some linker-specific flags for the target "test" (these flags are
related to
emscripten)
# cmake_minimum_required(VERSION major.minor) cmake_minimum_required(VERSION 3.8) #project(<NAME>) project(someProjectName) set (CMAKE_CXX_STANDARD 17) add_executable(test src/test.cpp) set_target_properties( test PROPERTIES LINK_FLAGS "--bind -s WASM=1 -s MODULARIZE=1 -s ALLOW_MEMORY_GROWTH=1 -s WASM_ASYNC_COMPILATION=0 -s BINARYEN_ASYNC_COMPILATION=0 -s ENVIRONMENT=shell" )
Emscripten, which allows us to compile C++ to wasm, acts
as a kind of wrapper around your software build system.
Once you've installed it, if you are using CMake,
the recommended way of doing is:
Configure
emcmake cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S .
But you can also do the following:
EMSCRIPTEN_CMAKE_PATH=${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN_CMAKE_PATH} .
In the end you should have your compile_commands.json generated.
Build
emmake make
Sometimes make is sufficient, but I still don't know why :
chances are emcmake already configured the build system
to use emscripten.
Clangd
From now on, clangd should be able to parse includes from emscripten
and LSP features should work.