链接到的静态库 [英] linking against a static library

查看:114
本文介绍了链接到的静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc Version: 4:4.4.4-1ubuntu2
GNU Make 3.81

我叫下面的库 net_api.a 和一些头文件,即

network_set.h

我有包括我的源$ C ​​$ C头文件在我的main.c文件

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

在我的Makefile中我曾尝试使用以下,code片段链接:

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’

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

您必须处理的第一个问题就是为什么code未编译。有一个在你的 network_set.h 头一个问题;它不是自足以某种方式,所以你必须有别的东西,包括它之前,或者必须以某种方式明确地配置。你的目标应该有你的头既自成一体,幂等。

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.


  • 自足可以包含没有任何其他的头preceding它

  • 可以包含多次,而不会造成混乱

  • self-contained can be included without any other headers preceding it
  • idempotent can be included multiple times without causing chaos

自包容通过确保它可以是包括在一个源文件中的第一报头取得,然后编译干净。这意味着,如果它使用的功能(例如,为size_t ),那么它包括一个标题定义功能(例如,&LT; stddef。 H&GT;

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 */

我用下面的脚本,名为 chkhdr ,以确保头是独立的和幂等的。

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

例如:

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

链接

在适当的时候,你已经固定编译问题后,你会遇到连接问题。选项​​ -lnet_api 查找名为 libnet_api.so 图书馆或 libnet_api.a

要与 net_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} ...

当然,你可以定义为路径整个图书馆的宏。请注意我是如何在宏观LIB_DIR方面重新定义LIB_PATH。

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.

这篇关于链接到的静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆