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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351926a4623884.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信