我在Windows 10 64bit的C VS项目中拥有这些文件:
main.c:
#include "exercise.h"
#include <stdio.h>
#include <string.h>
int main() {
unsigned int res[1000] = { 0 };//result array
const char* file_path = "22195_26468.txt";
printf("%d", detect_correlation_anomalies(file_path, res));
for (int i = 0;i < 1000;i++)
{
printf("%d ", res[i]);
}
return(0);
}
exercise.h:
int detect_timing_anomalies(const char* file_path, unsigned int *anomalies_ids);
int detect_behavioral_anomalies(const char* file_path, unsigned int *anomalies_ids);
int detect_correlation_anomalies(const char* file_path, unsigned int *anomalies_ids);
exercise.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "exercise.h"
#define MAXINPUT 101 //line max length.
#define MAXOUTPUT 1000 //max length of *anomalies_ids.
//struct for saving a message details.
typedef struct
{
unsigned int id;
unsigned int time;
unsigned int ecu;
unsigned int val1;
unsigned int val2;
} message;
//structs for different types of messages.
message speed, pedals, ABS, tire, temp;
//open file in read mode, return 1 if successful, else 0.
int openFile(FILE **file, const char *file_path)
{
//try to open the file
fopen_s(file, file_path, "r");
if (*file == NULL) //if the file couldn't be open, return 0,else return 1.
return 0;
return 1;
}
//put the current msg details in temp struct (parse the msg).
void parseMsg(char str[MAXINPUT])
{
char *token;
char *next_token = NULL;
unsigned int offset;
token = strtok_s(str, " ", &next_token);
sscanf_s(token, "%u", &temp.id);
token = strtok_s(NULL, " ", &next_token);
sscanf_s(token, "%u", &temp.time);
token = strtok_s(NULL, " ", &next_token);
sscanf_s(token, "%x", &temp.ecu);
token = strtok_s(NULL, " ", &next_token);
sscanf_s(token, "%u", &temp.val1);
token = strtok_s(NULL, " ", &next_token);
sscanf_s(token, "%u", &temp.val2);
}
//copy the current msg details to the proper struct, saving it for comparison
//later.
copyMsgToStruct(message *strct)
{
strct->id = temp.id;
strct->time = temp.time;
strct->ecu = temp.ecu;
strct->val1 = temp.val1;
strct->val2 = temp.val2;
}
...
在Windows中可以正常编译。
我基本上只需要确保我的文件在linux下正确编译即可。
因此,我只需要能够在Linux上运行该程序...就足够简单了,我想...哦,我错了。
这就是为什么我需要首先安装WSL并尝试所有(某些)解决方案以使其运行的麻烦,但无济于事。
我尝试使用以下指南安装WSL:https://docs.microsoft.com/en-us/windows/wsl/install-win10
安装了Ubuntu。 我在Windows中有BASH,能够在VS的WSL中使用ssh连接到linux。
创建了一个新的linux项目,尝试放入相同的文件,但是在尝试编译时出现以下错误:
exercise.c: In function ‘openFile’:
exercise.c:25:2: warning: implicit declaration of function ‘fopen_s’ [-Wimplicit-function-declaration]
fopen_s(file, file_path, "r");
^
exercise.c: In function ‘parseMsg’:
exercise.c:36:10: warning: implicit declaration of function ‘strtok_s’ [-Wimplicit-function-declaration]
token = strtok_s(str, " ", &next_token);
^
exercise.c:36:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
token = strtok_s(str, " ", &next_token);
^
exercise.c:37:2: warning: implicit declaration of function ‘sscanf_s’ [-Wimplicit-function-declaration]
sscanf_s(token, "%u", &temp.id);
^
exercise.c:38:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
token = strtok_s(NULL, " ", &next_token);
^
exercise.c:40:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
token = strtok_s(NULL, " ", &next_token);
^
exercise.c:42:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
token = strtok_s(NULL, " ", &next_token);
^
exercise.c:44:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
token = strtok_s(NULL, " ", &next_token);
^
exercise.c:35:15: warning: unused variable ‘offset’ [-Wunused-variable]
unsigned int offset;
^
exercise.c: At top level:
exercise.c:49:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
copyMsgToStruct(message *strct)
^
exercise.c: In function ‘TimingSpeedometer’:
exercise.c:62:15: warning: comparison between pointer and integer
if (speed.id != NULL)//first speedometer msg - no problem.
^
exercise.c: In function ‘TimingPedals’:
exercise.c:70:16: warning: comparison between pointer and integer
if (pedals.id != NULL)//first pedals msg - no problem.
^
exercise.c: In function ‘TimingABS’:
exercise.c:78:13: warning: comparison between pointer and integer
if (ABS.id != NULL)//first ABS msg - no problem.
^
exercise.c:82:18: warning: passing argument 1 of ‘copyMsgToStruct’ from incompatible pointer type [-Wincompatible-pointer-types]
copyMsgToStruct(&abs);
^
exercise.c:49:1: note: expected ‘message * {aka struct <anonymous> *}’ but argument is of type ‘__attribute__((const)) int (*)(int)’
copyMsgToStruct(message *strct)
^
exercise.c: In function ‘TimingTires’:
exercise.c:86:14: warning: comparison between pointer and integer
if (tire.id != NULL)//first tires msg - no problem.
^
exercise.c: In function ‘copyMsgToStruct’:
exercise.c:56:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/tmp/cclahunW.o: In function `openFile':
exercise.c:(.text+0x29): undefined reference to `fopen_s'
/tmp/cclahunW.o: In function `parseMsg':
exercise.c:(.text+0x80): undefined reference to `strtok_s'
exercise.c:(.text+0xa1): undefined reference to `sscanf_s'
exercise.c:(.text+0xbc): undefined reference to `strtok_s'
exercise.c:(.text+0xdd): undefined reference to `sscanf_s'
exercise.c:(.text+0xf8): undefined reference to `strtok_s'
exercise.c:(.text+0x119): undefined reference to `sscanf_s'
exercise.c:(.text+0x134): undefined reference to `strtok_s'
exercise.c:(.text+0x155): undefined reference to `sscanf_s'
exercise.c:(.text+0x170): undefined reference to `strtok_s'
exercise.c:(.text+0x191): undefined reference to `sscanf_s'
collect2: error: ld returned 1 exit status
也许我没有正确地放置它们,使用整个WSL令我望而生畏,在Google上搜索1件事给了我大量信息,我不知道是什么。
我在Linux中安装了GCC。 我是linux的完全新手,不知道Internet上所有复杂术语的含义是什么,只是其中的一部分。 我试图使用以下命令从bash编译这3个文件:
gcc -Wall main.c exercise.c -o main
但是我得到了同样的错误。 我认为linux(或称其为“ GCC编译器”?)不“知道”这些引起问题的通用库stdio.h,stdlib.h和string.h。
有人知道如何解决此问题吗? 我可能错过了一些细节,因为现在是凌晨2点,并且在2个小时的时间里仔细研究如何使WSL正常工作后,我有些头晕。
如果有我未写的信息,请告诉我,我会在大约10个小时后添加... 非常感谢你!