【发布时间】:2014-04-20 17:33:59
【问题描述】:
我有几个文件无法一起编译。这可能是我试图编译它们的方式是错误的。但我就是不能让他们一起工作。 我尝试了几种不同的更改,但尚未弄清楚是什么导致它们无法一起编译。
我使用 Windows 和 Microsoft Visual Studio 以及开发人员命令提示符作为我的编译器。
我对尝试将多个文件一起编译仍然是新手。
对于defs.h 文件,我得到了LNK1107:invalid or corrupt file: cannot read at 0x3E7 error。
对于 pack.c 和 unpack.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.o和unpacked.o作为目标文件,这些文件可以链接到程序中(使用main()函数)。
标签: c file compiler-construction linker