c mac OS X上的代码编译错误

时间:2015-03-22 02:20:53

标签: c macos compiler-errors osx-yosemite

我正在尝试在我的macbook pro上编译一个简单的c代码(yosmite 10.10.2,Xcode 6.2)。

#include <stdio.h>
#include "htslib/sam.h"

int main(int argc, char *argv[]){

    htsFile *fp_in = NULL;

    fp_in = hts_open(argv[1],"r");

    if(NULL == fp_in){
        printf("can't open file\n");
    }

    return 0;
}

但是我有编译错误。

gcc -g -Wall -v test_bam.c -o test_bam

Undefined symbols for architecture x86_64:
  "_hts_open", referenced from:
      _main in test_bam-7d7369.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我在/ usr / local / include目录下有所有必需的头文件。我google了很多,这似乎是一个简单的链接问题。我尝试用-L参数编译仍然有同样的问题。然后我尝试使用-arch i386,但它也不起作用。我也试过clang作为类似帖子之一,但同样的问题。

经过六个小时的努力,我在这里发帖。

感谢。

1 个答案:

答案 0 :(得分:1)

如果你想要包含 htslib / sam.h 标题,你必须查看/ usr / local / include文件夹,所以添加 -I / usr / local / include 到您的编译器标志。

此外,您还必须将c文件的第2行更改为:

#include <htslib/sam.h>

Angular braces意味着在系统目录中搜索头文件,而带引号的语法意味着在本地路径中搜索。

假设你的hts库安装在/ usr / local / lib /中,你可以添加 -L / usr / local / lib 标志。

最后添加 -lhts 标志以链接libhts库。

gcc -g -Wall -L/usr/local/lib -I/usr/local/lib -lhts test_bam.c -o test_bam
相关问题