Single LLDB (static linking)

Hello!

Has anyone tried building liblldb statically?
I wrote “add_lldb_library(liblldb STATIC …”, but I got a lot of DLL related errors when linking.

Can you tell me how to fix this?

I’m sorry, I have to provide additional information. It turned out that under Linux and macOS, building in STATIC mode works fine. Doesn’t work on Windows only. Here is an example of linking errors that appear on Windows.

Linking CXX executable bin\lldb-vscode.exe
FAILED: bin/lldb-vscode.exe 
cmd.exe /C "cd . && C:\mingw64\bin\clang++.exe -femulated-tls -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -ffunction-sections -fdata-sections -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -O3 -DNDEBUG -fuse-ld=lld -static -lpthread -Wl,--stack,16777216    -Wl,--gc-sections @CMakeFiles\lldb-vscode.rsp -o bin\lldb-vscode.exe -Wl,--out-implib,lib\liblldb-vscode.dll.a -Wl,--major-image-version,0,--minor-image-version,0  && cd ."
ld.lld: error: undefined symbol: __declspec(dllimport) lldb::SBDebugger::Initialize()
>>> referenced by tools/lldb/tools/lldb-vscode/CMakeFiles/lldb-vscode.dir/lldb-vscode.cpp.obj:(main)
ld.lld: error: undefined symbol: __declspec(dllimport) lldb::SBDebugger::Terminate()
>>> referenced by tools/lldb/tools/lldb-vscode/CMakeFiles/lldb-vscode.dir/lldb-vscode.cpp.obj:(main)
ld.lld: error: undefined symbol: __declspec(dllimport) lldb::SBError::SBError()
>>> referenced by tools/lldb/tools/lldb-vscode/CMakeFiles/lldb-vscode.dir/lldb-vscode.cpp.obj:((anonymous namespace)::request_attach(llvm::json::Object const&))
>>> referenced by tools/lldb/tools/lldb-vscode/CMakeFiles/lldb-vscode.dir/lldb-vscode.cpp.obj:((anonymous namespace)::request_attach(llvm::json::Object const&))
>>> referenced by tools/lldb/tools/lldb-vscode/CMakeFiles/lldb-vscode.dir/lldb-vscode.cpp.obj:((anonymous namespace)::request_launch(llvm::json::Object const&))
>>> referenced 10 more times
ld.lld: error: undefined symbol: __declspec(dllimport) lldb::SBAttachInfo::SBAttachInfo()
>>> referenced by tools/lldb/tools/lldb-vscode/CMakeFiles/lldb-vscode.dir/lldb-vscode.cpp.obj:((anonymous namespace)::request_attach(llvm::json::Object const&))

It should be possible to make this work by defining the LLDB_API preprocessor macro to an empty string (something like add_definitions(-DLLDB_API=)).
However, note that this is not an officially supported configuration. If you do this, you’re on your own.

CMake Warning:
Manually-specified variables were not used by the project:

LLDB_API

I analyzed the sources, and LLDB_API is not found anywhere.

I also tried:

  add_definitions(-DLLDB_API=)
  add_definitions(LLDB_API=)

This leads to an error:

FAILED: tools/lldb/source/API/CMakeFiles/liblldb.dir/SBAddress.cpp.obj
clang++: error: no such file or directory: ‘LLDB_API=’

Maybe it has something to do with LLDB_IN_LIBLLDB define?

By the way. I build under Windows not with cl, but with clang.

In the LLDB source code there following things:

#ifndef LLDB_API
#if defined(_WIN32)
#if defined(LLDB_IN_LIBLLDB)
#define LLDB_API __declspec(dllexport)
#else
#define LLDB_API __declspec(dllimport)
#endif
#else // defined (_WIN32)
#define LLDB_API
#endif
#endif

Every class or function declaration uses LLDB_API macro for defining __declspec(dllimport) OR __declspec(dllexport) on windows related platforms for proper dynamic linking with created .dll libraries

You need to redefine LLDB_API macro to nothing when you building lldb related tools with liblldb staticly

I think the best way to doing that is:

target_compile_definitions(liblldb PUBLIC LLDB_API=)

Thanks a lot!

target_compile_definitions really helped! I even tried add_compile_definitions before declaring liblldb but unfortunately didn’t work. Possibly because add_lldb_library works outside the context of the current CMakeLists.txt

P.S. I also want to say thank you for finding LLDB_API. I searched before, but didn’t find it - it turns out I was setting the wrong file filter :slight_smile:

I have tested single lldb-vscode on several operating systems. Everywhere the lldb-vscode binary starts correctly, under Windows and macOS debugging works correctly. But under Linux, debugging doesn’t work, it requires a dependency on lldb-server, although I don’t use remote debugging. How to fix it?

You can test it yourself if you want, here is my Linux binary: https://github.com/powertech-center/storage/raw/master/lldb/powerlldb-linux-x64.7z

lldb requires lldb-server even for local debugging and we don’t currently have any way to get rid of that requirement. Windows is an exception because it does support lldb-server-less debugging. And debugging on macos presumably works because your lldb was able to find the debugserver (that’s the mac equivalent of lldb-server) in some system directory.

Thank you
I have lldb-server next to lldb-vscode but the same error appears. Where is lldb-server looked for? Can I somehow configure it to look for a server nearby? Or maybe it’s possible to build lldb without depending on lldb-server just like under Windows?

The static linking breaks the assumptions that lldb uses to locate lldb-server. That’s what I meant when I said you’re on your own. :confused: Setting the LLDB_DEBUGSERVER_PATH environment variable helps.

That’s not possible.

So I need to initialize LLDB_DEBUGSERVER_PATH before starting lldb-vscode? Hm. I don’t see the env/environment setting for debuggers in package.json )