简体   繁体   中英

How to compile a static library and link to it using g++?

I've been trying to install a library (gzstream), which consists of one .C, one .h and an appropriate makefile. To be able to use #include <gzstream.h> , which gzstream.C uses, I've put the gzstream.h file in /usr/local/include and the gzstream.C in /usr/local/lib .

When I try to compile aufgabe2.cpp, I get the following error message on the terminal. aufgabe2.cpp:1:22: fatal error: /usr/local/include/gzstream.h: Permission denied compilation terminated.

What am I doing wrong?

Before being able to use the static library, you need to compile it. This will require you to cd to the directory where the source code for gzstream is present and then type make .

This will compile the library and create an output file libgzstream.a .

Once this is ready, you can include the header file and compile your code. There is no strict need to copy the gzstream.h into /usr/local/include. It may as well reside in the local directory where your source code is present. Then it can be easily included with

#include "gzstream.h" 

See how double quotes are used instead of the angular brackets to indicate relative path in current directory.

The g++ command line should be something like this.

g++ aufgabe2.cpp -L. -lgzstream -lz

-L. tells the linker to look for the static library in the current directory. This assumes that libgzstream.a is copied to your source directory where aufgabe2.cpp is present. If not, then give the relative path to the -L argument where libgzstream.a is present.

Arguments -lgzstream and -lz ask the linker to link these libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM