gpt4 book ai didi

C 程序 — 将文件编译在一起时出现问题

转载 作者:行者123 更新时间:2023-11-30 21:11:25 25 4
gpt4 key购买 nike

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

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

对于尝试将多个文件编译在一起,我还是新手。对于 defs.h 文件,我收到 LNK1107:无效或损坏的文件:无法读取 0x3E7 错误

对于 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

最佳答案

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

要证明或反驳这一理论,请重命名 packed更改为不同的名称,例如 Packed看看是否有帮助。在 defs.h 中执行此操作和packed.c 。如果错误消息的变化不仅仅是重命名操作的结果,这会有所帮助。如果该消息说了有关 packed 的内容之前然后说Packed相反,这并没有帮助。如果错误消息发生重大变化(或根本不再存在),则packed关键字是麻烦。

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

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

我可以确认您的defs.h文件可以在带有 GCC 4.8.2 的 Mac OS X 10.9.2 上干净地编译。我有一个脚本,可以对 header 进行检查编译以测试自给自足性和幂等性。实际上,它创建了一个源文件(例如 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 .

奥托,MSDN显示您需要使用 #pragma pack(2) ,例如,打包结构。这也不能偶然使用。尽管如此,当包含缺少的 header 时,您显示的代码可以在 Unix 系统上干净地编译。如果它不能在 MSVC 中干净地编译,则问题是 MSVC 环境固有的,而不是一般 C 语言中固有的问题。

关于C 程序 — 将文件编译在一起时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23185545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com