【问题标题】:c code compilation error on mac OS Xmac OS X上的c代码编译错误
【发布时间】:2015-03-23 01:42:23
【问题描述】:

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

#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 目录下拥有所有必要的头文件。我用谷歌搜索了很多,这似乎是一个简单的链接问题。我尝试使用 -L 参数进行编译,但我仍然遇到同样的问题。然后我尝试了 -arch i386 ,但它也不起作用。我也尝试过 clang 作为建议的类似帖子之一,但同样的问题。

挣扎了六个小时后,我在这里发帖。

谢谢。

【问题讨论】:

  • 你应该使用-l而不是-L
  • 嗨,我也试过-l,但它说'sam.h' not found!。 gcc -g -Wall -L /usr/local/include/htslib/ -lsam test_bam.c -o test_bamtest_bam.c:12:10: fatal error: 'sam.h' file not found #include "sam.h"
  • 使用-I 包含搜索路径,-L 用于库搜索路径,-l 用于链接库。

标签: c macos compiler-errors osx-yosemite


【解决方案1】:

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

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

#include <htslib/sam.h>

尖括号表示在系统目录中搜索头文件,而带引号的语法表示在本地路径中搜索。

假设您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多