Search code examples
c++parameterscompilationg++

How to use/create libraries in C++ without using compiler parameters?


I mainly code in Python, but now I want to start to code in C++. In Python, you can just

import glfw # example

I want to do the same in C++. I know I can do it like this:

#include <glfw.h>

But then I have to use parameters in the compiler.

What I tried:

  • Look up a single Stack Overflow question

What I'm expecting:

  • Use libraries in C++ without specifying parameters. I want to have it like this: g++ main.cpp

Solution

  • Welcome to C++. A complete answer goes down a rabbit hole, so I'll simplify.

    The statement #include <glfw.h> uses <>, which technically means the header file is outside the local project. Typically, this means it is a library provided by the compiler distribution. It may be a third-party library you installed on your system.

    If you use "", the compiler will search the same directory as your source file and then for the compiler or third-party files.

    Nothing is needed on the command line for the compiler or local headers. A third-party library probably does need an -I indicating where the header is located. There are some variations to this, depending on the compiler and OS.

    ----- update

    Despite the OP liking this response, others do not, so I'll go down the aforementioned rabbit hole. Some of this elaborates on other comments, so please recognize their contributions.

    The OP is using a third-party library. The compiler does not know the directory of the header or, shared or static library files. Technically, the latter two are for the linker, but we'll ignore that detail since all are handled on the command line.

    The directory of headers is provided by the -I<directory> option.

    Shared libraries are not compiled into the binary of your program but exist as separate files. Static libraries are included in the program's binary. Library files require both the name and the location. These options are -l<library name> and `-L. Specifying the directory for a shared library may not be necessary since each OS has a default location for shared files.

    There is only a simple edge case where you can compile without providing command line options. This is why package managers are mentioned in many of the comments. One of their purposes is to handle the details of creating the command line with the myriad required options.