如何安装 C++ 库以便我可以使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1069602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I install a c++ library so I can use it?
提问by rzetterberg
I have this library called BASSwhich is an audio library which I'm going to use to record with the microphone. I have all the files needed to use it, but I don't know how to install the library. I tried taking the example files and putting them in the same directory as the bass.h file. But I got a bunch of errors saying there are function calls that doesn't exist.
我有一个叫做BASS的库,它是一个音频库,我将用它来用麦克风录音。我拥有使用它所需的所有文件,但我不知道如何安装该库。我尝试将示例文件放在与 bass.h 文件相同的目录中。但是我收到了一堆错误,说有不存在的函数调用。
So my question is, how do I install it to be able to use it?
所以我的问题是,如何安装它才能使用它?
采纳答案by Francesco
Installing a C++ library means specifying to interested software (eg. a compiler) the location of two kinds of files: headers (typical extensions *.h or .hpp) and compiled objects (.dll or *.lib for instance).
安装 C++ 库意味着向感兴趣的软件(例如编译器)指定两种文件的位置:头文件(典型的扩展名 *.h 或.hpp)和编译对象(例如.dll 或 *.lib)。
The headers will contain the declarations exposed to the developer by the library authors, and your program will #include them in its source code, the dll will contain the compiled code which will be or linked together and used by your program, and they will be found by the linker (or loaded dynamically, but this is another step).
头文件将包含库作者向开发人员公开的声明,您的程序将在其源代码中#include 它们,dll 将包含编译后的代码,这些代码将被或链接在一起并由您的程序使用,它们将被由链接器找到(或动态加载,但这是另一个步骤)。
So you need to
所以你需要
- Put the header files in a location which your compiler is aware of (typically IDE allows to set so-called include directories, otherwise you specify a flag like "-I" when invoking the compiler)
- Put the dll files in a location which your linker is aware of (surely your IDE will allow that, otherwise you speficy a flag like "-L -l"
- 将头文件放在编译器知道的位置(通常 IDE 允许设置所谓的包含目录,否则您在调用编译器时指定像“-I”这样的标志)
- 将 dll 文件放在您的链接器知道的位置(您的 IDE 肯定会允许这样做,否则您指定一个像“-L -l”这样的标志)
Last but not least, since I see that BASS library is a commercial product, probably they will have made available some installation instructions?
最后但并非最不重要的一点,既然我看到 BASS 库是一个商业产品,他们可能会提供一些安装说明?
回答by Nasser
See the code below code and don not forget to put bass.dll in the directory of your exe file and include the file bass.lib with your project and don not forget also to include the path to bass.h and bass.lib in the default include and lib path of your project.
请参阅下面的代码,不要忘记将bass.dll 放在您的exe 文件的目录中,并将文件bass.lib 包含在您的项目中,并且不要忘记在其中包含bass.h 和bass.lib 的路径项目的默认包含和 lib 路径。
#include <iostream>
#include "bass.h"
using namespace std;
int main(int argc, const char **argv)
{
if (!BASS_Init(-1, 44100, 0, NULL ,NULL))
{
cout<<"Can't initialize device";
return -1;
}
int stream = BASS_StreamCreateFile(false, "D:\mypro\Trans_Langs\germ\quran_amma\Translations\Sound_aya\Sora1\Hafs\basfar\a7.mp3", 0L, 0L, 0);
if (stream != 0)
{
// play the stream channel
BASS_ChannelPlay(stream, false);
}
else
{
// error creating the stream
cout<<"Stream error: {0}", BASS_ErrorGetCode();
}
getchar();
BASS_StreamFree(stream);
// free BASS
BASS_Free();
return 0;
}
回答by Alex Payne
Run this command in a terminal or console.
在终端或控制台中运行此命令。
cpp -v
Notice at the end of the output, you'll see a line like this:
请注意,在输出的末尾,您将看到如下一行:
#include<...> search starts here:
There will be a list of directories below that line. Move the package folder to one of those directories. Then try importing the module with <>.
该行下方将有一个目录列表。将包文件夹移动到这些目录之一。然后尝试使用 <> 导入模块。
回答by Arjun Singri
If there are files named configure
, Makefile
or install
you can try running them in that order. After that, any program that wants to link with this library must use a command like this:
如果有名为 的文件configure
,Makefile
或者install
您可以尝试按该顺序运行它们。之后,任何想要链接到这个库的程序都必须使用这样的命令:
c++ <your_program.cpp> -l<library_name> -L<path_where_library_is_installed>
The library path is usually the original library folder itself, unless you explicitly change it or the library itself puts its files in global locations like /usr/local
or something like that.
库路径通常是原始库文件夹本身,除非您明确更改它或库本身将其文件放在全局位置/usr/local
或类似的位置。