我有几个文件,我无法一起编译。可能是我试图编译它们的方式可能是错误的。但我无法让他们一起工作。 我已经尝试了几种不同的更改,但还没有找出导致它们不能一起编译的原因。
我使用Windows和Microsoft Visual Studio以及开发人员命令提示符作为我的编译器。
我还想尝试一起编译多个文件。
对于defs.h
文件,我获得了LNK1107:invalid or corrupt file: cannot read at 0x3E7 error
。
对于pack.c
和unpack.c
,它有一个语法错误,在第15行中说标识符unpack
缺少分号和结束括号,以及第23行相同的错误但标识符{ {1}}而不是
我想包装和解压文件,因为我在pack
文件中使用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
#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
答案 0 :(得分:0)
我认为您遇到了麻烦,因为packed
是一个可以应用于结构的属性,表明它们应该存储在结构元素之间没有填充。这或多或少与您指出的错误消息一致。
要证明或反驳这一理论,请将packed
重命名为其他名称,例如Packed
并查看是否有帮助。在defs.h
和packed.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 <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中。