Linux gcc/g++ compiles and links header files and libraries (dynamic library.so and static library.a)

When I was studying the log4cpp library recently, I used g++ to compile it, but found that I couldn't link it..., how could this work, so I studied it online and finally solved it. Now record it and share it with people who encounter the same problem.


gcc is similar to g++, here we take g++ as an example!

The log4cpp log library just used has header files, dynamic library .so and static library .a, here we take the log4cpp library as an example.

After installing the log4cpp library, there are lib libraries and include header files under the path /usr/local/

NOTE: The default installation path for any library is here!

Then create a new folder as the project folder, enter this folder, then create a new include folder, copy the log4cpp folder in the above picture to the newly created include folder; then create a new lib folder, and copy the liblog4cpp. *Copy all to the newly created lib folder;

The final effect is as follows:

 Then write the mian function according to your own project requirements.

Then you can start compiling!

g++ MyLog.cpp test_log4cpp.cpp -std=c++11 -I ./include/ -L ./lib/ -llog4cpp -lpthread -o test_log4cpp

In this way, the link can be compiled normally!


introduce

-std=c++11 : indicates that the project uses c++11;

-I ./include/ : Specify the header file path used by the project, so that the header file used in the project does not need to write the full path;

                        For example: (otherwise you have to include "./include/log4cpp/Category.hh" and use it like this)

                        

-L ./lib/ : Specify the library path of the project link;

-llog4cpp : link liblog4cpp.so library; (link dynamic library and static library are used in this way, default link dynamic library)

                If you need to link a static library, you can change the name of the static library, for example, to liblog4cpp1.a, and then you can link the static library like this: -llog4cpp1

The specific path specified by -I -L is specified according to the path of the header file and library stored in your own project! 


Specific parameter introduction: 

-L : Indicates the directory where the library to be linked is located. -L means that the library to be linked is in the current directory, and -L /usr/lib means that the library to be linked is under /usr/lib. When the directory is in /usr/lib, the system will automatically search for this directory, no need to specify. When the library is not under /usr/lib, you have to use -L to specify the path of the library to be linked, otherwise it cannot be linked normally during compilation, and the compilation will fail!

-l (lowercase L)  : Indicates the name of the library to be linked. Note that it is not the name of the library file. For example, if the library file is liblog4cpp.so, then the library name is log4cpp; the same is true for static libraries!

-I (uppercase i)  : Specifies the directory where the header file is located, and a relative path can be used. You don't need to specify the specific path to use the header file in the code.

For example:

                


Note again:

If the dynamic library has the same name as the static library, for example: liblog4cpp.so and liblog4cpp.a

When using -llog4cpp to link this library, the default link is the dynamic library (.so). If you need to link the static library, you can delete the dynamic library, or change the name of the static library, for example, to liblog4cpp1.a; When linking again: -llog4cpp1 can be correctly connected to the static library.

in addition:

If you need to link multiple header file paths or library paths, you can continue to use -I and -L to link , for example:

g++ ./commonLog/MyLogger.cpp MyLog.cpp test_log4cpp.cpp -std=c++11 -I ./include/ -I ./commonLog/ -L ./lib/ -L ./commonLog/ -llog4cpp1 -lpthread -o test_log4cpp

episode

How does c++ call the c library?

The following c code is compiled into a library:

c_ku.h

#ifndef _C_KU_H_
#define _C_KU_H_

#include <stdio.h>


void print_hello();
void say_hello(char *str);

#endif // _C_KU_H_

c_ku.c

#include "c_ku.h"

void print_hello() {
    printf("hello world!\n");
}

void say_hello(char *str) {
    if (!str) {
        return;
    }

    printf("say %s\n", str);
}

Compile into dynamic library (.so) and static library (.a)

gcc -c c_ku.c
ar -rsv libc_ku.so c_ku.o
ar -rsv libc_ku.a c_ku.o

 main.cpp : c++ file

#include <iostream>
#include "c_ku.h"

int main(int argc, char **argv) {

    std::cout << "hello world!" << std::endl;

    print_hello();

    return 0;

}

Then compile and link:

g++ main.cpp -I ./ -L ./ -lc_ku -o c_ku

At this time, an error will be found:

 
solution:

Use extern "C"{} in the c header file  to wrap the header file declaration!

#ifndef _C_KU_H_
#define _C_KU_H_

#include <stdio.h>

extern "C"{
    void print_hello();
    void say_hello(char *str);
}

#endif _C_KU_H_

Compile and run again:

Problem solved perfectly!

C++ can call libraries written in C.

Guess you like

Origin blog.csdn.net/cpp_learner/article/details/128753411