gpt4 book ai didi

c - 为 Windows 编译二进制文件的问题

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

我正在尝试从 github 编译代码:https://github.com/ayeks/SGX-hardware/blob/master/test-sgx.c

我有 Visual Studio 2010,并且正在使用 Visual Studio 的命令行版本 (cl) 进行编译,它给出了以下编译错误:

sgx_check.c(6) : error C2054: expected '(' to follow 'inline'
sgx_check.c(8) : error C2085: 'native_cpuid' : not in formal parameter list
sgx_check.c(8) : error C2143: syntax error : missing ';' before '{'
sgx_check.c(88) : error C2143: syntax error : missing ';' before 'type'
sgx_check.c(89) : error C2065: 'i' : undeclared identifier
sgx_check.c(89) : error C2065: 'i' : undeclared identifier
sgx_check.c(89) : error C2065: 'i' : undeclared identifier
sgx_check.c(91) : error C2065: 'i' : undeclared identifier
sgx_check.c(91) : error C2065: 'i' : undeclared identifier
sgx_check.c(93) : error C2065: 'i' : undeclared identifier

用于编译代码的命令是:

cl sgx_check.c

我检查了编译器错误消息,但无法修复它们。

我可以通过将“int i”行移动到 main() 函数的开头来修复“i”作为未声明标识符的错误消息。

我想了解如何编译这个二进制文件的所有细节。

最佳答案

使用Linux

我编译了链接代码:

这是结果:

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled2.c" 
untitled2.c: In function ‘main’:
untitled2.c:93:10: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
ecx = i;
^
untitled2.c:32:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc, char **argv)
^~~~
untitled2.c:32:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
int main(int argc, char **argv)
^~~~
Compilation finished successfully.

鉴于这个结果,我有一些建议:

  1. 当参数为main()时不会被使用,那么使用签名:int main( void )
  2. 这些行:int i; for (i=2; i<10; i++) {应该 for( unsigned i=2; i<10; i++) {

应用上述修复后:

#include <stdio.h>
#if defined(_MSC_VER)
#include <intrin.h>
#endif

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
/* ecx is often an input as well as an output. */

#if !defined(_MSC_VER)

asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));

#else
int registers[4] = {0,0,0,0};

__cpuidex(registers, *eax, *ecx);
*eax = registers[0];
*ebx = registers[1];
*ecx = registers[2];
*edx = registers[3];

#endif
}

int main( void )
{
/* This programm prints some CPUID information and tests the SGX support of the CPU */

unsigned eax, ebx, ecx, edx;
eax = 1; /* processor info and feature bits */

native_cpuid(&eax, &ebx, &ecx, &edx);
printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);

printf("stepping %d\n", eax & 0xF); // Bit 3-0
printf("model %d\n", (eax >> 4) & 0xF); // Bit 7-4
printf("family %d\n", (eax >> 8) & 0xF); // Bit 11-8
printf("processor type %d\n", (eax >> 12) & 0x3); // Bit 13-12
printf("extended model %d\n", (eax >> 16) & 0xF); // Bit 19-16
printf("extended family %d\n", (eax >> 20) & 0xFF); // Bit 27-20

// if smx set - SGX global enable is supported
printf("smx: %d\n", (ecx >> 6) & 1); // CPUID.1:ECX.[bit6]

/* Extended feature bits (EAX=07H, ECX=0H)*/
printf("\nExtended feature bits (EAX=07H, ECX=0H)\n");
eax = 7;
ecx = 0;
native_cpuid(&eax, &ebx, &ecx, &edx);
printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);

//CPUID.(EAX=07H, ECX=0H):EBX.SGX = 1,
printf("sgx available: %d\n", (ebx >> 2) & 0x1);

/* SGX has to be enabled in MSR.IA32_Feature_Control.SGX_Enable
check with msr-tools: rdmsr -ax 0x3a
SGX_Enable is Bit 18
if SGX_Enable = 0 no leaf information will appear.
for more information check Intel Docs Architectures-software-developer-system-programming-manual - 35.1 Architectural MSRS
*/

/* CPUID Leaf 12H, Sub-Leaf 0 Enumeration of Intel SGX Capabilities (EAX=12H,ECX=0) */
printf("\nCPUID Leaf 12H, Sub-Leaf 0 of Intel SGX Capabilities (EAX=12H,ECX=0)\n");
eax = 0x12;
ecx = 0;
native_cpuid(&eax, &ebx, &ecx, &edx);
printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);

printf("sgx 1 supported: %d\n", eax & 0x1);
printf("sgx 2 supported: %d\n", (eax >> 1) & 0x1);
printf("MaxEnclaveSize_Not64: %x\n", edx & 0xFF);
printf("MaxEnclaveSize_64: %x\n", (edx >> 8) & 0xFF);

/* CPUID Leaf 12H, Sub-Leaf 1 Enumeration of Intel SGX Capabilities (EAX=12H,ECX=1) */
printf("\nCPUID Leaf 12H, Sub-Leaf 1 of Intel SGX Capabilities (EAX=12H,ECX=1)\n");
eax = 0x12;
ecx = 1;
native_cpuid(&eax, &ebx, &ecx, &edx);
printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);


for ( unsigned i=2; i<10; i++) {
/* CPUID Leaf 12H, Sub-Leaf i Enumeration of Intel SGX Capabilities (EAX=12H,ECX=i) */
printf("\nCPUID Leaf 12H, Sub-Leaf %d of Intel SGX Capabilities (EAX=12H,ECX=%d)\n",i,i);
eax = 0x12;
ecx = i;
native_cpuid(&eax, &ebx, &ecx, &edx);
printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);
}
}

运行程序的结果是:

eax: 630f01 ebx: 1040800 ecx: 3e98320b edx: 178bfbff
stepping 1
model 0
family 15
processor type 0
extended model 3
extended family 6
smx: 0

Extended feature bits (EAX=07H, ECX=0H)
eax: 0 ebx: 9 ecx: 0 edx: 0
sgx available: 0

CPUID Leaf 12H, Sub-Leaf 0 of Intel SGX Capabilities (EAX=12H,ECX=0)
eax: 0 ebx: 0 ecx: 0 edx: 0
sgx 1 supported: 0
sgx 2 supported: 0
MaxEnclaveSize_Not64: 0
MaxEnclaveSize_64: 0

CPUID Leaf 12H, Sub-Leaf 1 of Intel SGX Capabilities (EAX=12H,ECX=1)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 2 of Intel SGX Capabilities (EAX=12H,ECX=2)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 3 of Intel SGX Capabilities (EAX=12H,ECX=3)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 4 of Intel SGX Capabilities (EAX=12H,ECX=4)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 5 of Intel SGX Capabilities (EAX=12H,ECX=5)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 6 of Intel SGX Capabilities (EAX=12H,ECX=6)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 7 of Intel SGX Capabilities (EAX=12H,ECX=7)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 8 of Intel SGX Capabilities (EAX=12H,ECX=8)
eax: 0 ebx: 0 ecx: 0 edx: 0

CPUID Leaf 12H, Sub-Leaf 9 of Intel SGX Capabilities (EAX=12H,ECX=9)
eax: 0 ebx: 0 ecx: 0 edx: 0

这是在 AMD A8-7650k radeon r7、10 个计算核心 4c+6g x 4 上运行

关于c - 为 Windows 编译二进制文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54676834/

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