简体   繁体   中英

How to include third party libraries in C++ windows?

I have the boost library in my downloads folders. When I tried to include a particular file. It is throwing errors. Below is the code and the steps I did.

\\main.cpp

#include "type_index.hpp"

int main(){
//some code
return 0;
}

I opened the command prompt and ran the following command

g++ -IC:\Users\Owner\Downloads\boost_1_70_0\boost -o main main.cpp

I got the following error in command prompt

In file included from main.cpp:2:0:
C:\Users\Owner\Downloads\boost_1_70_0\boost/type_index.hpp:17:28: fatal error: boost/config.hpp: No such file or directory
 #include <boost/config.hpp>
                            ^
compilation terminated.

How can I run the above file? Do I have to change the location of boost directory from downloads folder to some where within mingw directories?

Adding the picture of directory: 在此处输入图片说明

Assuming boost is correctly configured and built on your system, there will be a location where the hub of the boost include root is located. Ex: if you downloaded and built boost in c:\\Stuff\\boost_1_70_0 , then within that folder will be the hub of the boost include set, c:\\Stuff\\boost_1_70_0\\boost , and it contains all of the boost headers.

boost is referenced by amending the include path to provide access to the boost include hub; not to provide access to the top-most headers in the hub. Similar to openssl, boost prefaces all of their header includes in their own headers, with boost/ . The consumers of boost should do the same, Therefore, the include path must include the folder where the boost/ hub can be found. It should not include the boost/ hub itself as part of the path.

Ex: This is correct

g++ -Ic:\Stuff\boost_1_70_0 -o main main.cpp

This, on the other hand is wrong:

g++ -Ic:\Stuff\boost_1_70_0\boost -o main main.cpp

With the former, when code includes:

#include <boost/asio.hpp>

the include path is searched, and the file is found. Further, within that header, when the compiler see this:

#include <boost/asio/associated_allocator.hpp>

it can still resolve correctly, because dropping that "thing" on the end of one of the folders in the include path works.

Now, consider the wrong case. What happens if you configure the include path to accidentally specify the boost/ root hub itself? Well, now you can do this:

#include <asio.hpp>

But as soon as the preprocessor starts in on that header it will see:

#include <boost/asio/associated_allocator.hpp>

Um.. woops. The pre-processor will look for this and never find it

Summary

When using boost headers in your source, you always refer to them with the boost hub preamble:

#include <boost/headername.hpp>

and always include the folder where the boost/ hub is located in your build configuration as an amended include path; not full path including the boost/ hub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM