链接静态库
海湾合作委员会版本:4:4.4.4-1ubuntu2 GNU Make 3.81
我有以下名为 net_api.a
的库和一些头文件,即
network_set.h
我已将头文件包含在我的 main.c 文件中的源代码中,
#include <network_set.h>
我有以下静态库和头文件以下目录
./tools/net/lib/net_api.a
./tools/net/inc/network_set.h
在我的 Makefile 中,我尝试使用以下代码片段进行链接:
INC_PATH = -I tools/net/inc
LIB_PATH = -L tools/net/lib
LIBS = -lnet_api
$(TARGET): $(OBJECT_FILES)
$(CC) $(LDFLAGS) $(CFLAGS) $(INC_PATH) $(LIB_PATH) $(LIBS) $(OBJECT_FILES) -o $(TARGET)
main.o: main.c
$(CC) $(CFLAGS) $(INC_PATH) $(LIB_PATH) -c main.c
但是,当我编译时,出现以下错误:
network_set.h error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘network_String’
这里出了什么问题?
gcc Version: 4:4.4.4-1ubuntu2
GNU Make 3.81
I have the following library called net_api.a
and some header files i.e.
network_set.h
I have include the header file in my source code in my main.c file
#include <network_set.h>
I have the following static library and header in the following directory
./tools/net/lib/net_api.a
./tools/net/inc/network_set.h
In my Makefile I have tried to link using the following, code snippet:
INC_PATH = -I tools/net/inc
LIB_PATH = -L tools/net/lib
LIBS = -lnet_api
$(TARGET): $(OBJECT_FILES)
$(CC) $(LDFLAGS) $(CFLAGS) $(INC_PATH) $(LIB_PATH) $(LIBS) $(OBJECT_FILES) -o $(TARGET)
main.o: main.c
$(CC) $(CFLAGS) $(INC_PATH) $(LIB_PATH) -c main.c
However, when I compile I get the following errors:
network_set.h error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘network_String’
What is going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编译
您必须处理的第一个问题是为什么代码无法编译。您的
network_set.h
标头有问题;它在某种程度上不是独立的,因此您必须在包含它之前包含其他内容,或者必须以某种方式显式配置它。您的目标应该是让您的标头既独立又幂等。自包含是通过确保它可以是包含在其中的第一个标头来实现的源文件,然后干净地编译。这意味着如果它使用某个功能(例如,
size_t
),那么它会包含一个定义该功能的标头(例如,
)。幂等性是通过包含标头防护来实现的:
我使用以下名为 chkhdr 的脚本来确保标头是独立的且幂等的。
例如:
链接
在适当的时候,在修复了编译问题之后,您将遇到链接问题。选项
-lnet_api
查找名为libnet_api.so
或libnet_api.a
的库。要与 net_api.a 链接,您必须将文件的路径名传递给链接命令:
显然,您可以为整个库的路径定义一个宏。请注意我如何根据宏 LIB_DIR 重新定义 LIB_PATH。
Compiling
The first problem you have to deal with is why the code is not compiling. There is a problem in your
network_set.h
header; it is not self-contained in some way, so you have to include something else before including it, or you have to explicitly configure it in some way. You should aim to have your headers both self-contained and idempotent.Self-containment is achieved by ensuring it can be the first header included in a source file and then compiles cleanly. It means that if it uses a feature (for example,
size_t
) then it includes a header that defines the feature (for example,<stddef.h>
).Idempotence is achieved by including a header guard:
I use the following script, called
chkhdr
, to ensure that headers are self-contained and idempotent.For example:
Linking
In due course, after you've fixed the compilation problems, you will run into linking problems. The option
-lnet_api
looks for a library namedlibnet_api.so
orlibnet_api.a
.To link with
net_api.a
, you will have to pass the pathname to the file to the link command:Obviously, you could define a macro for the path to the whole library. Note how I redefined LIB_PATH in terms of the macro LIB_DIR.
标头
network_set.h
具有必须首先包含的额外依赖项,其中之一是network_String
的定义。检查库文档或咨询作者以获取更多详细信息。The header
network_set.h
has extra dependencies that must be included first, one of which is the definition ofnetwork_String
. Check the library documentation or consult the author for more details.你不显示你的 LDFLAGS;我认为它们已被定义,但您只是没有发布它们。如果您正在针对静态库进行构建,则它们必须包含“-static”。
如果您不知道它们是什么,请查看以“gcc”开头的编译器输出,看看那里是否显示“-static”。
You don't show your LDFLAGS; I assume they are defined but you just didn't post them. They must include "-static" if you're building against a static library.
If you don't know what they are, look at the compiler output at the start where it begins with "gcc" and see if "-static" shows up there.