Nguyễn Thu Phương
Nguyễn Thu Phương

Reputation: 45

A non-painful way to compile lzma library on Windows with Cmake/CLion?

I have spent 2 days on this problem, and still have no idea how to do it.

All I am trying to do is build a program, or rather function, in C that would take in a compressed lzma file, and extract it on Windows. I am doing it with CLion, with Cygwin 64 3.0.1.

Sound easy? Probably, except for the fact that all method I tried just simple does not work.

Let me get this out of the way first that I am not familiar with Cmake, so that is pretty much why I am asking for advice.

Methods I have tried:

1)

Searching many code on github, I simply see them do

#include <lzma.h>

Nice, just does not work on Windows. When I compile, I got the error:

undefined reference to `lzma_stream_decoder'
undefined reference to `lzma_code'

Googling tell me that I am lacking the library for lzma development, and the solution is simply do a sudo apt, and... you guess it: not for Windows.

2)

Maybe it is a Cmake thing, so let's try to compile with simple gcc first. I grab a simple decompress code with #include <lzma.h> and tried to compile it on Ubuntu. Awesome, everything went smoothly.

Back to Windows, I opened up the Cygwin terminal. Oh of course In Cygwin, I have downloaded liblzma-devel.

Compile is running great to. BUT when firing up the executable:

cygwin1.dll and cyglzma-5.dll are missing

What the... Even when I specifically downloaded those files and put them to the same folder with the executable, it still fails to start correctly.

3)

Back to Clion/Cmake.

I have looked in to the Lzma SDK and no avail. What code should I write to the Cmake file? I cannot find any guide that says how to make use of this in C, let alone compile it using Cmake.

Googling "Cmake Lzma" lead me to the FindLibLZMA, which I have no idea what they are writing about. I still have no clue what line to put into my CmakeList.txt file, like add_library or find_package... nope, nothing.

4)

Finally, Xz Utils... the most extensive Lzma tool as far as I can understand, and of course somehow, it also does not work.

I put the file FindLibLZMA.cmake (which I pull down from github) into a folder called Cmake. Then I added

find_package(LibLZMA REQUIRED)
include_directories(${LIBLZMA_INCLUDE_DIRS})

into the CmakeList.txt file. Then I set the LIBLZMA_INCLUDE_DIR in the FindLibLZMA.cmake file to the folder include of the Xz Utils for Windows... and u guess it, compilation error:

#   LIBLZMA_HAS_AUTO_DECODER  - True if lzma_auto_decoder() is found (required).
#   LIBLZMA_HAS_EASY_ENCODER  - True if lzma_easy_encoder() is found (required).
#   LIBLZMA_HAS_LZMA_PRESET   - True if lzma_lzma_preset() is found (required).

All these library or something are required, yet where are they? The compiler could not find it, so yeah... again, I am stuck.

I have been trying from yesterday, and I am still stuck.

So please, I am very tired now. Any kind soul would tell me how to make the Lzma library work with Cmake on Windows...Please...Please. Thank you!

Upvotes: 0

Views: 3207

Answers (2)

Zerte
Zerte

Reputation: 1516

If you are not too afraid by calling Ada from C, there is a standalone decoder for .lzma files in the open-source Zip-Ada project. The difficulty may be to find a relatively recent version of GNAT (the GCC compiler for Ada) running on Cygwin. The up-to-date versions for Windows are usually MinGW-ish.

Upvotes: 0

Nguyễn Thu Phương
Nguyễn Thu Phương

Reputation: 45

Thank you Some programmer dude.

For anyone happen to google this, in order to make Xz Utils (the method 4 I tried) to work on Cmake with Clion, I am being very 101 here for any newbie like me now:

1) Create a folder named Cmake in your project source folder.

2) Put the file FindLibLZMA.Cmake into that folder.

3) Add these lines to your CMakeList.txt

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/Cmake")
find_package(LibLZMA REQUIRED)
include_directories(${LIBLZMA_INCLUDE_DIRS})
target_link_libraries(DukasCompiler ${LIBLZMA_LIBRARIES})

4) Open the FindLibLZMA.Cmake, and at the beginning, add

set(LIBLZMA_INCLUDE_DIR "LINK")

with "LINK" is the link to the include folder that you have downloaded from XZ Utils for Windows.

And you are good to go.

THAT BEING SAID: lzma_stream_decoder only accept .xz file, not .lzma file. So yeah, I am back to square one.

Upvotes: 1

Related Questions