Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shared Library Build Target #412

Closed
abetlen opened this issue Mar 22, 2023 · 12 comments · Fixed by #430
Closed

Add Shared Library Build Target #412

abetlen opened this issue Mar 22, 2023 · 12 comments · Fixed by #430
Labels
bug Something isn't working build Compilation issues enhancement New feature or request

Comments

@abetlen
Copy link
Collaborator

abetlen commented Mar 22, 2023

With the C API now merged it would be very useful to have build targets for make and cmake that produce shared library versions of llama.cpp. This way llama.cpp can just be dynamically linked in other applications.

@gjmulder gjmulder added enhancement New feature or request build Compilation issues labels Mar 22, 2023
@thomasantony
Copy link

You can already do that by passing in -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=On to cmake.

@abetlen
Copy link
Collaborator Author

abetlen commented Mar 23, 2023

EDIT: It works but I also needed to add the -DCMAKE_CXX_FLAGS=-fPIC and -DCMAKE_C_FLAGS=-fPIC to avoid the error below.

@thomasantony thanks, I wasn't aware of those flags, however this doesn't seem to be working for me.

Running the following from the llama.cpp root directory

mkdir build
cd build
cmake .. -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=On
make

gives me a linking error (maybe I'm missing an additional flag?)

image

However, if I add a shared library build target to the CMakeLists.txt and run it without the flags everything builds without issue

add_library(llamacpp SHARED
            llama
            ggml)

@Green-Sky Green-Sky added the bug Something isn't working label Mar 23, 2023
@Green-Sky
Copy link
Collaborator

this is supposed to work, so I'm marking this as a bug.

EDIT: It works but I also needed to add the -DCMAKE_CXX_FLAGS=-fPIC and -DCMAKE_C_FLAGS=-fPIC to avoid the error below.

cmake needs fixing.

you should not need to link against ggml agains here :)

add_library(llamacpp SHARED
            llama
            ggml)

@hanss0n
Copy link

hanss0n commented Apr 2, 2023

I'm still having issues with GLIBC dependencies when compiling this as a library

/usr/bin/ld: CMakeFiles/test-quantize.dir/test-quantize.c.o: undefined reference to symbol 'roundf@@GLIBC_2.2.5' /usr/bin/ld: /lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line

Any clue if this is also a bug or if I'm missing something? Chain of command is:

mkdir build && cd build
cmake .. -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=ON
make

Which fails with this error

@hanss0n
Copy link

hanss0n commented Apr 2, 2023

Seems like something fails with the import of "math.h" intest-quantize

@Green-Sky
Copy link
Collaborator

try to link with math (-lm)

@kambo-1st
Copy link

Linking with math certainly helps, yet I don't know if this is a proper way:

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 157d733..5767b8b 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,7 +1,7 @@
 function(llama_add_test source)
     get_filename_component(TEST_TARGET ${source} NAME_WE)
     add_executable(${TEST_TARGET} ${source})
-    target_link_libraries(${TEST_TARGET} PRIVATE llama)
+    target_link_libraries(${TEST_TARGET} PRIVATE llama m)
     add_test(NAME ${TEST_TARGET} COMMAND $<TARGET_FILE:${TEST_TARGET}> ${ARGN})
 endfunction()

@Green-Sky
Copy link
Collaborator

I wonder why this is only sometimes a problem.

@ghost
Copy link

ghost commented Apr 7, 2023

It's worth noting that for me I'm seeing linking issues if using BUILD_SHARED_LIBS with OpenBLAS enabled.

mkdir build && cd build
cmake .. -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=ON -DLLAMA_OPENBLAS=ON
make

<REDACTED>:build$ ld libllama.so 
ld: warning: cannot find entry symbol _start; not setting start address
ld: libllama.so: undefined reference to `cblas_sgemm'

OpenBLAS was not linked properly and libllama was not usable. I had to link it in CMakeLists.txt:247 target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS} openblas) to get libllama.so to properly reference the lib.

@CameronTofer
Copy link

I think you need to specify the options before the path. This worked for me:
cmake -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=ON -DLLAMA_OPENBLAS=ON ..

@MichaelrMentele
Copy link

Dumb question, I'm a C/C++ n00b -- after I run cmake -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=ON -DLLAMA_OPENBLAS=ON .. I don't see a library file. How do you statically link an artifact in another project? Trying to link llama.cpp to bindings in an scons project.

@lmbelo
Copy link
Contributor

lmbelo commented Jan 20, 2025

Dumb question, I'm a C/C++ n00b -- after I run cmake -DLLAMA_STATIC=Off -DBUILD_SHARED_LIBS=ON -DLLAMA_OPENBLAS=ON .. I don't see a library file. How do you statically link an artifact in another project? Trying to link llama.cpp to bindings in an scons project.

The "DBUILD_SHARED_LIBS=ON" option will make it a shared library, so dynamically linked only. If you need to statically link it, set "DBUILD_SHARED_LIBS=OFF" and you'll get a static library file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working build Compilation issues enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants