最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to usecreate libraries in C++ without using compiler parameters? - Stack Overflow

programmeradmin1浏览0评论

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

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
Share Improve this question edited Jan 17 at 20:58 JaMiT 17.3k5 gold badges18 silver badges39 bronze badges asked Jan 17 at 17:35 Marlo MannheimMarlo Mannheim 11 bronze badge 11
  • 4 use a package manager like vcpkg or conan and a build system like cmake or premake, stop trying to invoke g++ yourself, this gets too messy very quickly. – Ahmed AEK Commented Jan 17 at 17:37
  • @AhmedAEK Is there an alternative? – Marlo Mannheim Commented Jan 17 at 17:37
  • 1 No, building c++ projects with many dependencies is a complicated process, a build system makes it as simple as cmake --build . and you're done, so long as you have one or two more files that specify your dependencies and the project files. this is a basic skill that you need if you are going to use C++ – Ahmed AEK Commented Jan 17 at 17:42
  • 1 Even in Python you to use something like pip (possibly in a virtual env) to "teach" the interpreter what that import means. It's ludicrous to expect 0 setup for 3rd party libraries. – StoryTeller - Unslander Monica Commented Jan 17 at 19:37
  • 2 Stop... take one step back. And it might sound stupid, but fet everything you know about python, and start from scratch e.g. learncpp (C++ really works differently) – Pepijn Kramer Commented Jan 17 at 20:53
 |  Show 6 more comments

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论