Leela-zero: How to compile a static executable for other linux system?

Created on 9 May 2018  路  4Comments  路  Source: leela-zero/leela-zero

I've tried 'make' and 'cmake' in my local system, it works well.
But the executable cannot run on some linux system that doesn't have libboost library.
So I need to compile a static executable.

I tried to put these in CMakeLists.txt line 72, and ran 'cmake'.

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(BUILD_SHARED_LIBRARIES OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")

The leelaz executable from 3M to 27M!
It seems like everything is going well!
But when I exec it, error happened.

Segmentation fault (core dumped)

What can I do? Help me plz.

techsupport

Most helpful comment

I can reproduce this on Ubuntu 16.04. It looks like one of the used libraries doesn't like a static link.

Okay, you'll need another trick. Go back to the original Makefile, remove -march=native again, and make it read:

LIBS = -lboost_program_options
DYNAMIC_LIBS = -lpthread -lz

Remove the OpenCL reference later on.

Near the end

leelaz: $(objects)
        $(CXX) $(LDFLAGS) -o $@ $^ $(LIBS) $(DYNAMIC_LIBS)

to

leelaz: $(objects)
        $(CXX) $(LDFLAGS) -o $@ $^ -Wl,-Bstatic $(LIBS) -Wl,-Bdynamic $(DYNAMIC_LIBS)

This statically links boost but dynamically links everything else.

All 4 comments

If you use the Makefile in /src, just put everything from DYNAMIC_LIBS into LIBS instead, and add -static to CFLAGS and LDFLAGS.

Additionally, look for the -march=native flag in the Makefile and remove it.

The latter might also be the reason your CMake changes didn't work, i.e. remove that flag from the CMakeLists.txt as well.

My linux system doesn't have a gpu.
So I remove "#define USE_OPENCL" in config.h , and it work well originally.

But I tried your guide to compile, a error happened.

/usr/bin/ld: cannot find -lOpenCL

I tried to remove all the opencl library in Makefile, and it compile successfully!
But when I run leelaz, same error happened.

Segmentation fault (core dumped)

What should I do?

I can reproduce this on Ubuntu 16.04. It looks like one of the used libraries doesn't like a static link.

Okay, you'll need another trick. Go back to the original Makefile, remove -march=native again, and make it read:

LIBS = -lboost_program_options
DYNAMIC_LIBS = -lpthread -lz

Remove the OpenCL reference later on.

Near the end

leelaz: $(objects)
        $(CXX) $(LDFLAGS) -o $@ $^ $(LIBS) $(DYNAMIC_LIBS)

to

leelaz: $(objects)
        $(CXX) $(LDFLAGS) -o $@ $^ -Wl,-Bstatic $(LIBS) -Wl,-Bdynamic $(DYNAMIC_LIBS)

This statically links boost but dynamically links everything else.

Oh my god! It runs successfully!!!
Thank you really really much! You save my life!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

a270709570 picture a270709570  路  4Comments

l1t1 picture l1t1  路  4Comments

KID1412999 picture KID1412999  路  3Comments

l1t1 picture l1t1  路  3Comments

holicst picture holicst  路  4Comments