【问题标题】:Why am I getting error file format not reognized while compiling?为什么我在编译时收到错误文件格式无法识别?
【发布时间】:2018-07-10 21:48:27
【问题描述】:

我正在使用 libxml2 解析器来解析 xml 文件。但是当我使用 gcc 编译器编译时:

gcc test.c note.xml -I/usr/include/libxml2 -lxml2 -o output

它给了我以下错误:

/usr/bin/ld:note.xml: file format not recognized; treating as linker script
/usr/bin/ld:note.xml:1: syntax error
collect2: error: ld returned 1 exit status

这是我的 test.c 文件:

#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

void readfile(const char* filename) {
    xmlDocPtr doc;
    doc = xmlReadFile(filename,NULL,0);
    if(doc == NULL)
       return;
    xmlFreeDoc(doc);
}

int main(int argc,char* argv[]) {
    if(argc != 2)
        return 1;
    LIBXML_TEST_VERSION
    readfile(argv[1]);
    xmlCleanupParser();
    xmlMemoryDump();
    return 0; 
} 

这是我的 xml 文件:

<note>
  <to>Tove</to>
    <from>Jani</from>
      <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
   </note>

【问题讨论】:

  • 您无法编译 xml 文件。从命令行中删除 note.xml
  • 您在运行程序时需要该 .xml 文件。不是在你构建它时。
  • 您的readfile() 函数无需实际读取 XML 文件即可返回。但是,在您的 main() 函数中,未检查返回值,因此这些语句:xmlCleanupParser(); xmlMemoryDump(); 正在尝试处理尚未读取的 XML 文件。这是一个逻辑错误。强烈建议让readfile() 的错误退出和成功退出返回值main() 函数可以检查

标签: c libxml2


【解决方案1】:

错误说明了一切:

/usr/bin/ld:note.xml: file format not recognized; treating as linker script

文件格式无法识别。哪个文件? note.xml。是代码吗?不,它不是您的 C 代码的一部分。它根本不应该被编译,所以不要将它作为参数传递给gcc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多