链接静态库

发布于 2024-10-08 18:29:18 字数 833 浏览 1 评论 0原文

海湾合作委员会版本: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2024-10-15 18:29:18

编译

您必须处理的第一个问题是为什么代码无法编译。您的 network_set.h 标头有问题;它在某种程度上不是独立的,因此您必须在包含它之前包含其他内容,或者必须以某种方式显式配置它。您的目标应该是让您的标头既独立又幂等。

  • 自包含可以被包含,前面没有任何其他标头
  • 幂等可以被包含多次,而不会造成混乱

自包含是通过确保它可以是包含在其中的第一个标头来实现的源文件,然后干净地编译。这意味着如果它使用某个功能(例如,size_t),那么它会包含一个定义该功能的标头(例如,)。

幂等性是通过包含标头防护来实现的:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...main body of header...
#endif /* HEADER_H_INCLUDED */

我使用以下名为 chkhdr 的脚本来确保标头是独立的且幂等的。

#!/bin/ksh
#
# @(#)$Id: chkhdr.sh,v 1.2 2010/04/24 16:52:59 jleffler Exp $
#
# Check whether a header can be compiled standalone

tmp=chkhdr-$
trap 'rm -f $tmp.?; exit 1' 0 1 2 3 13 15

cat >$tmp.c <<EOF
#include HEADER /* Check self-containment */
#include HEADER /* Check idempotency */
int main(void){return 0;}
EOF

options=
for file in "$@"
do
    case "$file" in
    (-*)    options="$options $file";;
    (*)     echo "$file:"
            gcc $options -DHEADER="\"$file\"" -c $tmp.c
            ;;
    esac
done

rm -f $tmp.?
trap 0

例如:

chkhdr -Itools/net/inc tools/net/inc/network_set.h

链接

在适当的时候,在修复了编译问题之后,您将遇到链接问题。选项 -lnet_api 查找名为 libnet_api.solibnet_api.a 的库。

要与 net_api.a 链接,您必须将文件的路径名传递给链接命令:

LIB_DIR     = ./tools/net/lib
LIB_NET_API = net_api.a
LIB_PATH    = -L ${LIB_DIR}

    ${CC} ... ${LIB_DIR}/${LIB_NET_API} ...

显然,您可以为整个库的路径定义一个宏。请注意我如何根据宏 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-contained can be included without any other headers preceding it
  • idempotent can be included multiple times without causing chaos

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:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...main body of header...
#endif /* HEADER_H_INCLUDED */

I use the following script, called chkhdr, to ensure that headers are self-contained and idempotent.

#!/bin/ksh
#
# @(#)$Id: chkhdr.sh,v 1.2 2010/04/24 16:52:59 jleffler Exp $
#
# Check whether a header can be compiled standalone

tmp=chkhdr-$
trap 'rm -f $tmp.?; exit 1' 0 1 2 3 13 15

cat >$tmp.c <<EOF
#include HEADER /* Check self-containment */
#include HEADER /* Check idempotency */
int main(void){return 0;}
EOF

options=
for file in "$@"
do
    case "$file" in
    (-*)    options="$options $file";;
    (*)     echo "$file:"
            gcc $options -DHEADER="\"$file\"" -c $tmp.c
            ;;
    esac
done

rm -f $tmp.?
trap 0

For example:

chkhdr -Itools/net/inc tools/net/inc/network_set.h

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 named libnet_api.so or libnet_api.a.

To link with net_api.a, you will have to pass the pathname to the file to the link command:

LIB_DIR     = ./tools/net/lib
LIB_NET_API = net_api.a
LIB_PATH    = -L ${LIB_DIR}

    ${CC} ... ${LIB_DIR}/${LIB_NET_API} ...

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.

盗心人 2024-10-15 18:29:18

标头 network_set.h 具有必须首先包含的额外依赖项,其中之一是 network_String 的定义。检查库文档或咨询作者以获取更多详细信息。

The header network_set.h has extra dependencies that must be included first, one of which is the definition of network_String. Check the library documentation or consult the author for more details.

梦断已成空 2024-10-15 18:29:18

你不显示你的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文