【问题标题】:C program — trouble compiling files togetherC 程序 - 一起编译文件时遇到问题
【发布时间】:2014-04-20 17:33:59
【问题描述】:

我有几个文件无法一起编译。这可能是我试图编译它们的方式是错误的。但我就是不能让他们一起工作。 我尝试了几种不同的更改,但尚未弄清楚是什么导致它们无法一起编译。

我使用 Windows 和 Microsoft Visual Studio 以及开发人员命令提示符作为我的编译器。

我对尝试将多个文件一起编译仍然是新手。 对于defs.h 文件,我得到了LNK1107:invalid or corrupt file: cannot read at 0x3E7 error

对于 pack.cunpack.c,它有一个语法错误,在第 15 行标识符 unpack 缺少分号和结束括号,以及在第 23 行相同的错误但标识符 pack 代替

考虑到我在defs.h 文件中使用typedef,我考虑打包和解压缩文件,它不应该有标识符问题。

defs.h

#ifndef DEFS_H
#define DEFS_H

// a structure that contains field widths
typedef struct {
    int * fieldWidths; // a pointer to an array of bit field widths (in bits)
    int numWidths;     // the number of elements in the array (i.e., the number of bit fields)
} sizes;

// a structure that contains an array of ints containing packed data fields
typedef struct {
    int * fieldValues; // a pointer to an array of ints containing packed bit fields
    int n;             // the number of elements in the array
} packed;

// a structure that contains an array of ints containing individual data values (one per int)
typedef struct {
    int * values; // a pointer to an array of ints containing values for bit fields (one per element)
    int n; // the number of elements in the array
} unpacked;

packed pack(sizes s, unpacked un);
unpacked unpack(sizes s, packed p);

#endif

pack.c

#include "defs.h"

//The below #define is used to extract bits
//Usage: GETMASK(7, 3) returns value = 00000000_00000000_00000000_11111000
//Usage: GETMASK(7, 0) returns value = 00000000_00000000_00000000_11111111

#define GETMASK(lastbit, firstbit) ( (0xffffffff<<(firstbit)) & (0xffffffff>>(32-  (lastbit)-1) ) )
/*
* Pack values into bit fields.
*
* Parameters:
* s - The bit field widths.
* un - The unpacked values.
*
* Returns - packed values.
*/

packed pack(sizes s, unpacked un){

    packed p;

    int i=0, totalWidth=0, x=0;
    int shift;

// Calculating the max number of ints needed to store values
    for( i=0; i<s.numWidths; i++){
        totalWidth+=s.fieldWidths[i];
        p.n = ceil( totalWidth/32.0 );
        p.fieldValues = (int*)malloc( sizeof(int)*p.n );
        for( i=0; i<p.n; i++)
        p.fieldValues[i]=0;
    }

    shift=32;

    for( i=0; i<s.numWidths; i++){
        shift -= s.fieldWidths[i];
        if( shift < 0 ){
            int upperbits = s.fieldWidths[i] + shift;
            int part1 = un.values[i] & GETMASK(s.fieldWidths[i]-1, s.fieldWidths[i]-upperbits);
            int part2 = un.values[i] & GETMASK(s.fieldWidths[i]-upperbits-1, 0);
            p.fieldValues[x++] |= part1;
            shift += 32;
            p.fieldValues [x] |= (part2 << shift);
            continue;
        }
        p.fieldValues[x] |= (un.values[i] & GETMASK(s.fieldWidths[i]-1, 0)) << shift;
    }


return p;

} // end of pack function

解压.c

#include "defs.h"

/*
* Unpack values from bit fields.
*
* Parameters:
* s - The bit field widths.
* p - The packed values.
* 
* Returns - unpacked values.
*/

unpacked unpack(sizes s, packed p){

    unpacked up;

    int i=0, x;
    int index=0, temp;

    up.n = s.numWidths;
    up.values = (int*)malloc(sizeof(int) * p.n);

    x=0;
    index=0;
    temp = p.fieldValues[0];

    for( i=0; i<up.n; i++){
        if ( index + s.fieldWidths[i] > 32){
            int partb2 = (index+s.fieldWidths[i] - 32);
            int partb1 = s.fieldWidths[i] - partb2;
            int part1 = temp >> (32-partb1);
            up.values[i] = part1 << partb2;
            temp = p.fieldValues[++x];
            up.values[i] |= temp >> (32-partb2);
            temp <<= partb2;
            index =partb2;
            continue;
        }

        up.values[i] = temp >> (32-s.fieldWidths[i]);
        temp <<= s.fieldWidths[i];
        index += s.fieldWidths[i];
    }

return up;

} // end of unpack function

【问题讨论】:

  • 你没有说任何关于实际问题的事情。想象一下,如果有人来找你说“我的车坏了”。仅凭这句话,你能给他们多少帮助?
  • “不能一起编译”是什么意思?命令行?
  • 您可能应该确定您正在使用的环境(操作系统和编译器)。你当然应该展示你是如何编译它们的。您应该显示来自编译器的(前几个)错误消息。表面上,您可能会在类 Unix 机器上编写 cc -c packed.c unpacked.c 并期望得到 packed.ounpacked.o 作为目标文件,这些文件可以链接到程序中(使用 main() 函数)。

标签: c file compiler-construction linker


【解决方案1】:

我认为您遇到了麻烦,因为packed 是一个可以应用于结构的属性,以指示它们应该在结构元素之间没有填充的情况下存储。这或多或少与您指出的错误消息一致。

要证明或反驳这一理论,请将packed 重命名为其他名称,例如Packed,看看是否有帮助。在defs.hpacked.c 中执行此操作。如果错误消息的变化不仅仅是因为重命名操作,它会有所帮助。如果消息之前提到了packed,然后又说Packed,它没有帮助。如果错误消息发生重大变化(或根本不再存在),那么packed 关键字就是问题所在。

在 GCC 中,您需要使用诸如 __attribute__((packed)) 之类的符号来获得打包结构;你不能偶然使用它。

请注意,您通常不会编译标头;您编译使用标头的源文件。目前尚不清楚您为什么要尝试与标题链接。您链接目标文件和库。

我可以确认您的 defs.h 文件可以在带有 GCC 4.8.2 的 Mac OS X 10.9.2 上干净地编译。我有一个脚本对标头进行检查编译以测试自给自足和幂等性。实际上,它会创建一个源文件(例如,x3981f.c),其中包含(在这种情况下):

#include "defs.h"   // Self-sufficiency
#include "defs.h"   // Idempotency
int main(void) { return 0; }  // Non-empty file

它编译时没有错误或警告:

gcc -c -Wall -Wextra x3981f.c

pack.c 源文件需要 #include &lt;math.h&gt; 并且两个源文件都需要 #include &lt;stdlib.h&gt; 添加。但是,通过这些添加,代码可以使用这些更严格的编译选项进行干净的编译:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror -c pack.c
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror -c unpack.c
$

这实际上使我相信您的问题是名称packed

OTOH,MSDN 表明您需要使用#pragma pack(2),例如,打包结构。这也不能随便用。尽管如此,当包含缺少的标头时,您显示的代码在 Unix 系统上可以干净地编译。如果在 MSVC 中编译不干净,则问题出在 MSVC 环境中,而不是在 C 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    相关资源
    最近更新 更多