How to link Zlib with Cmake










0















I am trying to link my file with the zlib libray but still get: undefined reference to `deflateInit_'.



I am currently using CLion, have downloaded the zLib file from the homepage and added it into the project. This is how my CmakeLists.txt looks like



cmake_minimum_required(VERSION 3.10) project(GzipTest)

set(CMAKE_CXX_STANDARD 11)

include_directories(ZLIB zlib-1.2.11)

add_executable(GzipTest main.cpp zlib-1.2.11/zlib.h)


And the code (Copying from the zpipe.c):



include "iostream"

include "zlib.h"

include "iostream"

define CHUNK 1639


FILE *fp;


int def(FILE *source, FILE *dest, int level)
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

// Allocate Deflate state
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;





int main()
fp = fopen("inputFile.txt", "r");
if (fp == nullptr)
perror("Could not open data");
exit(EXIT_FAILURE);

def(fp, fp, 1);



What could be missing?
Thanks in advance










share|improve this question
























  • Have you compiled zlib? Where is libzlib.so or similar?

    – Kamil Cuk
    Nov 14 '18 at 10:55











  • You don't build with the zlib source file, or link with the zlib library.

    – Some programmer dude
    Nov 14 '18 at 10:56











  • How to compile zlib?

    – AKJ
    Nov 14 '18 at 10:57











  • You have doanloaded a package, there is a README in it with the instructions on how to build it on all platforms. You can also directly get a prebuilt package.

    – Matthieu Brucher
    Nov 14 '18 at 10:59











  • Matthieu, the readme: # To install libz.a, zconf.h and zlib.h in the system directories, type: # # make install -fwin32/Makefile.gcc.

    – AKJ
    Nov 14 '18 at 11:23
















0















I am trying to link my file with the zlib libray but still get: undefined reference to `deflateInit_'.



I am currently using CLion, have downloaded the zLib file from the homepage and added it into the project. This is how my CmakeLists.txt looks like



cmake_minimum_required(VERSION 3.10) project(GzipTest)

set(CMAKE_CXX_STANDARD 11)

include_directories(ZLIB zlib-1.2.11)

add_executable(GzipTest main.cpp zlib-1.2.11/zlib.h)


And the code (Copying from the zpipe.c):



include "iostream"

include "zlib.h"

include "iostream"

define CHUNK 1639


FILE *fp;


int def(FILE *source, FILE *dest, int level)
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

// Allocate Deflate state
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;





int main()
fp = fopen("inputFile.txt", "r");
if (fp == nullptr)
perror("Could not open data");
exit(EXIT_FAILURE);

def(fp, fp, 1);



What could be missing?
Thanks in advance










share|improve this question
























  • Have you compiled zlib? Where is libzlib.so or similar?

    – Kamil Cuk
    Nov 14 '18 at 10:55











  • You don't build with the zlib source file, or link with the zlib library.

    – Some programmer dude
    Nov 14 '18 at 10:56











  • How to compile zlib?

    – AKJ
    Nov 14 '18 at 10:57











  • You have doanloaded a package, there is a README in it with the instructions on how to build it on all platforms. You can also directly get a prebuilt package.

    – Matthieu Brucher
    Nov 14 '18 at 10:59











  • Matthieu, the readme: # To install libz.a, zconf.h and zlib.h in the system directories, type: # # make install -fwin32/Makefile.gcc.

    – AKJ
    Nov 14 '18 at 11:23














0












0








0








I am trying to link my file with the zlib libray but still get: undefined reference to `deflateInit_'.



I am currently using CLion, have downloaded the zLib file from the homepage and added it into the project. This is how my CmakeLists.txt looks like



cmake_minimum_required(VERSION 3.10) project(GzipTest)

set(CMAKE_CXX_STANDARD 11)

include_directories(ZLIB zlib-1.2.11)

add_executable(GzipTest main.cpp zlib-1.2.11/zlib.h)


And the code (Copying from the zpipe.c):



include "iostream"

include "zlib.h"

include "iostream"

define CHUNK 1639


FILE *fp;


int def(FILE *source, FILE *dest, int level)
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

// Allocate Deflate state
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;





int main()
fp = fopen("inputFile.txt", "r");
if (fp == nullptr)
perror("Could not open data");
exit(EXIT_FAILURE);

def(fp, fp, 1);



What could be missing?
Thanks in advance










share|improve this question
















I am trying to link my file with the zlib libray but still get: undefined reference to `deflateInit_'.



I am currently using CLion, have downloaded the zLib file from the homepage and added it into the project. This is how my CmakeLists.txt looks like



cmake_minimum_required(VERSION 3.10) project(GzipTest)

set(CMAKE_CXX_STANDARD 11)

include_directories(ZLIB zlib-1.2.11)

add_executable(GzipTest main.cpp zlib-1.2.11/zlib.h)


And the code (Copying from the zpipe.c):



include "iostream"

include "zlib.h"

include "iostream"

define CHUNK 1639


FILE *fp;


int def(FILE *source, FILE *dest, int level)
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

// Allocate Deflate state
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;





int main()
fp = fopen("inputFile.txt", "r");
if (fp == nullptr)
perror("Could not open data");
exit(EXIT_FAILURE);

def(fp, fp, 1);



What could be missing?
Thanks in advance







c cmake gzip zlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:58









Matthieu Brucher

16.3k32143




16.3k32143










asked Nov 14 '18 at 10:52









AKJAKJ

529




529












  • Have you compiled zlib? Where is libzlib.so or similar?

    – Kamil Cuk
    Nov 14 '18 at 10:55











  • You don't build with the zlib source file, or link with the zlib library.

    – Some programmer dude
    Nov 14 '18 at 10:56











  • How to compile zlib?

    – AKJ
    Nov 14 '18 at 10:57











  • You have doanloaded a package, there is a README in it with the instructions on how to build it on all platforms. You can also directly get a prebuilt package.

    – Matthieu Brucher
    Nov 14 '18 at 10:59











  • Matthieu, the readme: # To install libz.a, zconf.h and zlib.h in the system directories, type: # # make install -fwin32/Makefile.gcc.

    – AKJ
    Nov 14 '18 at 11:23


















  • Have you compiled zlib? Where is libzlib.so or similar?

    – Kamil Cuk
    Nov 14 '18 at 10:55











  • You don't build with the zlib source file, or link with the zlib library.

    – Some programmer dude
    Nov 14 '18 at 10:56











  • How to compile zlib?

    – AKJ
    Nov 14 '18 at 10:57











  • You have doanloaded a package, there is a README in it with the instructions on how to build it on all platforms. You can also directly get a prebuilt package.

    – Matthieu Brucher
    Nov 14 '18 at 10:59











  • Matthieu, the readme: # To install libz.a, zconf.h and zlib.h in the system directories, type: # # make install -fwin32/Makefile.gcc.

    – AKJ
    Nov 14 '18 at 11:23

















Have you compiled zlib? Where is libzlib.so or similar?

– Kamil Cuk
Nov 14 '18 at 10:55





Have you compiled zlib? Where is libzlib.so or similar?

– Kamil Cuk
Nov 14 '18 at 10:55













You don't build with the zlib source file, or link with the zlib library.

– Some programmer dude
Nov 14 '18 at 10:56





You don't build with the zlib source file, or link with the zlib library.

– Some programmer dude
Nov 14 '18 at 10:56













How to compile zlib?

– AKJ
Nov 14 '18 at 10:57





How to compile zlib?

– AKJ
Nov 14 '18 at 10:57













You have doanloaded a package, there is a README in it with the instructions on how to build it on all platforms. You can also directly get a prebuilt package.

– Matthieu Brucher
Nov 14 '18 at 10:59





You have doanloaded a package, there is a README in it with the instructions on how to build it on all platforms. You can also directly get a prebuilt package.

– Matthieu Brucher
Nov 14 '18 at 10:59













Matthieu, the readme: # To install libz.a, zconf.h and zlib.h in the system directories, type: # # make install -fwin32/Makefile.gcc.

– AKJ
Nov 14 '18 at 11:23






Matthieu, the readme: # To install libz.a, zconf.h and zlib.h in the system directories, type: # # make install -fwin32/Makefile.gcc.

– AKJ
Nov 14 '18 at 11:23













1 Answer
1






active

oldest

votes


















1














You have to link against zlib.



If you used:



find_package(ZLIB)


Then you should have:



target_link_libraries(GzipTest $ZLIB_LIBRARIES)


Also don't add the headers to your source files:



add_executable(GzipTest main.cpp)





share|improve this answer

























  • Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

    – AKJ
    Nov 14 '18 at 10:59












  • That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

    – Matthieu Brucher
    Nov 14 '18 at 11:00












  • @AKJ which version of cmake are you using?

    – Philipp
    Nov 14 '18 at 11:01











  • I am really a noob at this. Can you please instruct me on what to do?

    – AKJ
    Nov 14 '18 at 11:02











  • in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

    – Matthieu Brucher
    Nov 14 '18 at 11:04










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298492%2fhow-to-link-zlib-with-cmake%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have to link against zlib.



If you used:



find_package(ZLIB)


Then you should have:



target_link_libraries(GzipTest $ZLIB_LIBRARIES)


Also don't add the headers to your source files:



add_executable(GzipTest main.cpp)





share|improve this answer

























  • Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

    – AKJ
    Nov 14 '18 at 10:59












  • That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

    – Matthieu Brucher
    Nov 14 '18 at 11:00












  • @AKJ which version of cmake are you using?

    – Philipp
    Nov 14 '18 at 11:01











  • I am really a noob at this. Can you please instruct me on what to do?

    – AKJ
    Nov 14 '18 at 11:02











  • in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

    – Matthieu Brucher
    Nov 14 '18 at 11:04















1














You have to link against zlib.



If you used:



find_package(ZLIB)


Then you should have:



target_link_libraries(GzipTest $ZLIB_LIBRARIES)


Also don't add the headers to your source files:



add_executable(GzipTest main.cpp)





share|improve this answer

























  • Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

    – AKJ
    Nov 14 '18 at 10:59












  • That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

    – Matthieu Brucher
    Nov 14 '18 at 11:00












  • @AKJ which version of cmake are you using?

    – Philipp
    Nov 14 '18 at 11:01











  • I am really a noob at this. Can you please instruct me on what to do?

    – AKJ
    Nov 14 '18 at 11:02











  • in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

    – Matthieu Brucher
    Nov 14 '18 at 11:04













1












1








1







You have to link against zlib.



If you used:



find_package(ZLIB)


Then you should have:



target_link_libraries(GzipTest $ZLIB_LIBRARIES)


Also don't add the headers to your source files:



add_executable(GzipTest main.cpp)





share|improve this answer















You have to link against zlib.



If you used:



find_package(ZLIB)


Then you should have:



target_link_libraries(GzipTest $ZLIB_LIBRARIES)


Also don't add the headers to your source files:



add_executable(GzipTest main.cpp)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 11:00

























answered Nov 14 '18 at 10:56









Matthieu BrucherMatthieu Brucher

16.3k32143




16.3k32143












  • Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

    – AKJ
    Nov 14 '18 at 10:59












  • That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

    – Matthieu Brucher
    Nov 14 '18 at 11:00












  • @AKJ which version of cmake are you using?

    – Philipp
    Nov 14 '18 at 11:01











  • I am really a noob at this. Can you please instruct me on what to do?

    – AKJ
    Nov 14 '18 at 11:02











  • in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

    – Matthieu Brucher
    Nov 14 '18 at 11:04

















  • Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

    – AKJ
    Nov 14 '18 at 10:59












  • That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

    – Matthieu Brucher
    Nov 14 '18 at 11:00












  • @AKJ which version of cmake are you using?

    – Philipp
    Nov 14 '18 at 11:01











  • I am really a noob at this. Can you please instruct me on what to do?

    – AKJ
    Nov 14 '18 at 11:02











  • in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

    – Matthieu Brucher
    Nov 14 '18 at 11:04
















Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

– AKJ
Nov 14 '18 at 10:59






Should i replace $ZLIB_LIBRARIES with something? I get : CMake Error at CMakeLists.txt:9 (target_link_library): Unknown CMake command "target_link_library".

– AKJ
Nov 14 '18 at 10:59














That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

– Matthieu Brucher
Nov 14 '18 at 11:00






That's assuming that you have the package built, and that you are setting the paths to find zlib for cmake properly. And yes, fixed a type, should be librarIES, not librarY.

– Matthieu Brucher
Nov 14 '18 at 11:00














@AKJ which version of cmake are you using?

– Philipp
Nov 14 '18 at 11:01





@AKJ which version of cmake are you using?

– Philipp
Nov 14 '18 at 11:01













I am really a noob at this. Can you please instruct me on what to do?

– AKJ
Nov 14 '18 at 11:02





I am really a noob at this. Can you please instruct me on what to do?

– AKJ
Nov 14 '18 at 11:02













in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

– Matthieu Brucher
Nov 14 '18 at 11:04





in the cmake-gui (or ccmake on Linux, but in that case, you should already have a zlib package with the system), you need to fill the path to zlib as the errors that find_package indicates.

– Matthieu Brucher
Nov 14 '18 at 11:04



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298492%2fhow-to-link-zlib-with-cmake%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20