首页 > 解决方案 > Why libraries automatically added?

问题描述

I created a project in Visual Studio 2019. (Create a project > Windows Desktop Wizard > Console Application with all options unchecked.)

Screenshot

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);
}

标签: c++visual-studio-2019

解决方案


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.


推荐阅读