C 程序 — 一起编译文件时遇到问题

问题描述 投票:0回答:1

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

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

我对尝试将多个文件编译在一起仍然很陌生。 对于

defs.h
文件,我得到了
LNK1107:invalid or corrupt file: cannot read at 0x3E7 error

对于

pack.c
unpack.c
,它有一个语法错误,在第 15 行中缺少标识符
unpack
以及分号和结束括号,并且在第 23 行中存在相同的错误,但标识符为
pack

我想到了打包和解包文件,鉴于我在

typedef
文件中使用
defs.h
,它不应该有标识符问题。

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
c file linker
1个回答
0
投票

我认为您遇到了麻烦,因为

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

要证明或反驳这一理论,请将

packed
重命名为其他名称,例如
Packed
,看看这是否有帮助。 在
defs.h
packed.c
中进行。 如果错误消息的变化不仅仅是重命名操作的结果,这会有所帮助。 如果消息之前说了一些关于
packed
的内容,然后改为
Packed
,则没有帮助。 如果错误消息发生很大变化(或者根本不再存在),那么
packed
关键字就是麻烦所在。

在 GCC 中,您需要使用诸如

__attribute__((packed))
之类的符号来获取压缩结构;你不能偶然使用它。

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

我可以确认您的

defs.h
文件可以在 Mac OS X 10.9.2 和 GCC 4.8.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 <math.h>
并且两个源文件都需要添加
#include <stdlib.h>
。 然而,通过这些添加,代码可以使用这些更严格的编译选项进行干净的编译:

$ 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 语言中的问题。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.