简体   繁体   English

C 程序 — 无法将文件编译在一起

[英]C program — trouble compiling files together

I have several files that I can't get to compile together.我有几个文件无法一起编译。 It might be how I'm trying to compile them is wrong perhaps.这可能是我试图编译它们的方式可能是错误的。 But I just can't get them to work together.但我就是不能让他们一起工作。 I have tried several different changes and have yet to figure out what is causing them to not compile together.我尝试了几种不同的更改,但还没有弄清楚是什么导致它们无法编译在一起。

I'm using Windows and Microsoft Visual Studio and developer command prompt as my compiler.我使用 Windows 和 Microsoft Visual Studio 以及开发人员命令提示符作为我的编译器。

I'm still new to trying to compile several files together.我还是个新手,试图将几个文件编译在一起。 For the defs.h file, I'm getting a LNK1107:invalid or corrupt file: cannot read at 0x3E7 error .对于defs.h文件,我收到LNK1107:invalid or corrupt file: cannot read at 0x3E7 error

For pack.c and unpack.c it has a syntax error saying in line 15 identifier unpack is missing along with a semicolon and end parenthesis, along with in line 23 same error but identifier pack instead对于pack.cunpack.c它有一个语法错误说在第 15 行标识符unpack连同分号和结束括号一起丢失,以及在第 23 行相同的错误但标识符pack代替

I thought for pack and unpack files, given that I'm using typedef in the defs.h file, it shouldn't have the identifier problem.我想到了打包defs.h包文件,因为我在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包.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

unpack.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

I think you're running into trouble because packed is an attribute that can be applied to structures to indicate that they should be stored with no padding between structure elements.我认为您遇到了麻烦,因为packed是一个属性,可以应用于结构以指示它们应该在结构元素之间没有填充的情况下存储。 That is more or less consistent with the error messages you indicate.这或多或少与您指出的错误消息一致。

To prove or disprove this theory, rename packed to a different name, such as Packed and see whether that helps.为了证明或反驳这个理论,将packed重命名为不同的名称,例如Packed ,看看是否有帮助。 Do it in defs.h and packed.c .defs.hpacked.c It helps if the error messages change more than just as a result of the rename operation.如果错误消息的更改不仅仅是由于重命名操作的结果,它会有所帮助。 If the message said something about packed before and then says Packed instead, it hasn't helped.如果该消息提到了之前packed ,然后改为Packed ,则没有帮助。 If the error message changes substantially (or is no longer present at all), then the packed keyword is the trouble.如果错误消息发生了很大变化(或根本不再存在),那么packed关键字就是问题所在。

In GCC, you'd need to use a notation such as __attribute__((packed)) to get a packed structure;在 GCC 中,您需要使用诸如__attribute__((packed))类的符号来获取压缩结构; you can't use that by accident.你不能偶然使用它。

Note that you do not normally compile a header;请注意,您通常不会编译头文件; you compile the source files that use the header.您编译使用标头的源文件。 It is not clear why you would be trying to link with the header.目前尚不清楚您为什么要尝试与标题链接。 You link object files and libraries.您链接目标文件和库。

I can confirm that your defs.h file compiles cleanly on Mac OS X 10.9.2 with GCC 4.8.2.我可以确认您的defs.h文件在带有 GCC 4.8.2 的 Mac OS X 10.9.2 上编译干净。 I have a script that does a check compilation on a header to test for self-sufficiency and idempotency.我有一个脚本,它对标头进行检查编译以测试自给自足和幂等性。 Effectively, it creates a source file (for example, x3981f.c ) containing (in this case):实际上,它创建了一个包含(在本例中)的源文件(例如, x3981f.c ):

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

It compiles without errors or warnings with:它编译时没有错误或警告:

gcc -c -Wall -Wextra x3981f.c

The pack.c source file needs #include <math.h> and both source files need #include <stdlib.h> added. pack.c源文件需要#include <math.h>并且两个源文件都需要添加#include <stdlib.h> However, with those additions, the code compiles cleanly using these even more stringent compilation options:但是,通过这些添加,代码可以使用这些更严格的编译选项干净利落地编译:

$ 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
$

This practically convinces me that your problem is the name packed .这实际上使我相信您的问题是名称packed

OTOH, MSDN shows that you need to use #pragma pack(2) , for example, to pack structures. OTOH, MSDN显示您需要使用#pragma pack(2) ,例如,打包结构。 That too can't be used by accident.这也不能偶然使用。 Nevertheless, the code you show compiles cleanly on a Unix system when the missing headers are included.尽管如此,当包含缺少的头文件时,您显示的代码在 Unix 系统上可以干净地编译。 If it does not compile cleanly in MSVC, the problem is inherent in the MSVC environment, and not in C generically.如果它不能在 MSVC 中干净地编译,则问题是 MSVC 环境中固有的,而不是一般的 C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM