c++ - Why libraries automatically added?
问题描述
I created a project in Visual Studio 2019. (Create a project > Windows Desktop Wizard > Console Application with all options unchecked.)
Something like this code is working but I didn't add math.h library to the code. I don't understand why sqrt function is working without error.
#include <iostream>
int main()
{
std::cout << sqrt(9);
}
解决方案
Say you have the following files:
a.h
b.h
main.cpp
In main you have :
#include <a.h>
int main() {
// Use func from a.h
aFunc();
// Use func from b.h
bFunc();
}
Like your situation, let's say this builds no problem. How does the program know what bFunc
is then?
The likely answer is that a.h
looks like this :
#include <b.h>
aFunc();
That means that when you include a.h
you are also including b.h
from within.
Now if you are using b.h
functions specifically it's considered good practice to also include b.h
in main. If include guards are setup then there should be no problem and it'll be easier to read through the code.
推荐阅读
- c# - Azure EventGridClient 线程安全吗?
- bash - 用于在文本文件中搜索多个字符串的 Bash 脚本
- postgresql - 如何在 postgres 中设置复合类型属性的默认值?
- html - Contenteditable 在 iOS Safari 中不起作用
- html - 单击按钮时看不到我的侧边栏
- java - spring 中是否有任何内置方法来编写空/空检查方法?
- swift - 快速从iCloud驱动器上传pdf时出现“无效文件”错误
- angular - 如何以角度动态绑定树节点中的值
- macos - 如何在 macOS 上计算 APFS 卷上的“可清除”磁盘?
- tensorflow - ImportError:无法从 tensorflow.python.keras._impl.keras.backend 导入名称“abs”