不要用clang管理链接库

时间:2013-03-29 09:20:25

标签: c compiler-construction linker makefile clang

我想使用libuvhttp-parser构建一个网络服务器。

目前的项目结构是

Makefile

/src
  /main.c

/deps
  /libuv (git clone of libuv)
  /http-parser (git clone of http-parser)

在main.c中我定义了以下内容:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "uv.h"
#include "http_parser.h"

和Makefile看起来像:

LDFLAGS = -L deps/libuv

main: libuv http-parser
    $(CC) src/main.c -o main.out deps/libuv/libuv.a deps/http-parser/http_parser.o $(LDFLAGS)

libuv:
    $(MAKE) -C deps/libuv libuv.a

http-parser:
    $(MAKE) -C deps/http-parser http_parser.o

clean:
    rm deps/libuv/libuv.a
    rm deps/http-parser/http_parser.o

当编译器尝试链接http_parser时会发生这种情况。

make -C deps/libuv libuv.a
make[1]: `libuv.a' is up to date.
make -C deps/http-parser http_parser.o
make[1]: `http_parser.o' is up to date.
cc src/main.c -o main.out deps/libuv/libuv.a deps/http-parser/http_parser.o
src/main.c:5:10: fatal error: 'http_parser.h' file not found
#include "http_parser.h"
         ^
1 error generated.
make: *** [main] Error 1

当我从include和Makefile中删除http_parser时,我得到一个奇怪的libuv构建错误:

Undefined symbols for architecture x86_64:
  "_CFArrayCreate", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_CFRunLoopAddSource", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopGetCurrent", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopRemoveSource", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopRun", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopSourceCreate", referenced from:
      _uv__platform_loop_init in libuv.a(darwin.o)
  "_CFRunLoopSourceSignal", referenced from:
      _uv__cf_loop_signal in libuv.a(darwin.o)
  "_CFRunLoopStop", referenced from:
      _uv__platform_loop_delete in libuv.a(darwin.o)
  "_CFRunLoopWakeUp", referenced from:
      _uv__cf_loop_signal in libuv.a(darwin.o)
  "_CFStringCreateWithCString", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_CFStringGetSystemEncoding", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_FSEventStreamCreate", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_FSEventStreamInvalidate", referenced from:
      _uv__fsevents_close in libuv.a(fsevents.o)
  "_FSEventStreamRelease", referenced from:
      _uv__fsevents_close in libuv.a(fsevents.o)
  "_FSEventStreamScheduleWithRunLoop", referenced from:
      _uv__fsevents_schedule in libuv.a(fsevents.o)
  "_FSEventStreamStart", referenced from:
      _uv__fsevents_schedule in libuv.a(fsevents.o)
  "_FSEventStreamStop", referenced from:
      _uv__fsevents_close in libuv.a(fsevents.o)
  "_kCFRunLoopDefaultMode", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
      _uv__fsevents_schedule in libuv.a(fsevents.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

我从昨天起就陷入了这个问题,并且完全放弃了我的希望......

Makefile的当前状态:

LDFLAGS = -Ldeps/libuv
INCLFLAGS = -Ideps/http-parser

main.o: libuv.a http-parser.o
    $(CC) src/main.c $(INCLFLAGS) -o main.o deps/libuv/libuv.a deps/http-parser/http_parser.o $(LDFLAGS)

libuv.a:
    $(MAKE) -C deps/libuv libuv.a

http-parser.o:
    $(MAKE) -C deps/http-parser http_parser.o

clean:
    rm deps/libuv/libuv.a
    rm deps/http-parser/http_parser.o

详细输出:

#include "..." search starts here:
#include <...> search starts here:
 deps/libuv
 deps/http-parser
 /usr/local/include
 /usr/bin/../lib/clang/4.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o main.o -Ldeps/libuv /var/folders/4l/zj55m1gn289f6v04zfbl59bm0000gn/T/main-sRhCLM.o deps/libuv/libuv.a deps/http-parser/http_parser.o -lSystem /usr/bin/../lib/clang/4.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_CFArrayCreate", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_CFRunLoopAddSource", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)

1 个答案:

答案 0 :(得分:2)

我怀疑http_parser.h与main.c不在同一目录中。在那里复制该文件,或者最好更新INCLUDE路径。

HTTP_PARSER_INCLUDE= -I/home/wherever/http_parser

main: libuv http-parser
    $(CC) src/main.c -o main.out $(HTTP_PARSER_INCLUDE) deps/libuv/libuv.a deps/http-parser/http_parser.o $(LDFLAGS)

由于删除http_parser.h而导致的另一个错误可能只是由于未开始构建清理而产生的噪音。您在上面声明的“主要”规则构建“main.out”而不是“main”。这也可能与Make尝试无中生有地建立起来有关。

更新 - 您需要在命令行中添加-framework CoreFoundation以删除链接器错误。