简体   繁体   English

在C程序中包含一个库(lsusb)

[英]Including a library (lsusb) in a C program

I am still fairly new to programming with C and I am working on a program where I want to control the power to various ports on a hub I have. 对于使用C进行编程,我还是一个新手,我正在开发一个程序,在该程序中我想控制集线器上各个端口的电源。 That is, however, not the issue I am having right now. 但是,那不是我现在遇到的问题。

I found a program online that does what I want I am trying to compile it. 我在网上找到了一个程序,该程序可以完成我要编译的程序。 However it uses #include<lsusb.h> . 但是,它使用#include<lsusb.h> lsusb is located in a totally different folder than the file I am wanting to run (and not in a sub folder) and when I try to compile it, I, logically enough, get the error that the file lsusb.h is not found. lsusb与我要运行的文件位于一个完全不同的文件夹中(而不是子文件夹中),并且当我尝试对其进行编译时,从逻辑上来说,我得到的错误是找不到文件lsusb.h。

How can I link to this file so that it can be found? 如何链接到该文件以便可以找到它?

This is more of a GCC toolchain question than a C question (although most C compilers do use the same Unixy flags). 这比C问题更像是GCC工具链问题(尽管大多数C编译器确实使用相同的Unixy标志)。

The braces around the include file ( <> ) indicate you want the compiler to search its standard search path for the include file. 包含文件( <> )周围的花括号表示您希望编译器在其标准搜索路径中搜索包含文件。 So you can get access to that new include file either by putting it into a directory on your standard include file search path yourself, or by adding its directory to the file search path. 因此,您可以通过自己将其包含在标准包含文件搜索路径中的目录中,或将其目录添加到文件搜索路径中来访问该新包含文件。 With GCC you do the latter by giving gcc the flag -I"directoryname" where "directoryname" is the full file path to where you are keeping that new include file of yours. 使用GCC,您可以通过给gcc标记-I"directoryname"实现后者,其中“ directoryname”是保留新的包含文件的完整文件路径。

Once your compiler finds it, your linker may have the exact same problem with the library file itself ("liblsusb.a"?). 一旦编译器找到它,链接器就可能对库文件本身有完全相同的问题(“ liblsusb.a”?)。 You fix that the same way. 您以相同的方式修复。 The flag GCC's linker will want is -L instead of -I . 标志GCC的链接器将需要-L而不是-I

See the "-I" parameter in the gcc man page. 请参见gcc手册页中的“ -I”参数。 It allows you specify a directory in which to find a header file. 它允许您指定要在其中查找头文件的目录。 See also -l and -L. 另请参见-l和-L。

或尝试#include“ ../../path_to_the_file/lsusb.h”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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