我编写了一段简单的C代码,它使用sqlite3创建一个新数据库并向其添加用户输入的内容。当我在我的PC上编译x86(更改使用的db文件的目录)时,它编译并运行正常,当我交叉编译以在Raspberry Pi 2上使用时出现问题。它构建/编译正常但是当我尝试在Pi上运行可执行文件我得到的响应是“分段错误”,仅此而已。
这是我的代码,但我不相信这是代码问题:
#include <stdio.h>
#include <sqlite3.h>
int main()
{
sqlite3 *db;
int rc;
char *sql;
char *sql1;
char sql2[128];
char *zErrMsg = 0;
int DataToAdd = 1;
int TempID[64], TempReg[64], TempData[64];
int i = 0;
int j = 0;
char message[128];
rc = sqlite3_open("/home/testing/Basic.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return -1;
}else{
fprintf(stderr, "Opened database successfully\n");
}
printf("Do you have data to add? (1=Y, 0=N)\n");
scanf("%d", &DataToAdd);
while(DataToAdd){
if (DataToAdd) {
printf("Enter Modbus Device ID\n");
scanf("%d", &TempID[i]);
printf("Enter Register Number\n");
scanf("%d", &TempReg[i]);
printf("Enter Data Value\n");
scanf("%d", &TempData[i]);
}else {
printf("No more data to add...");
break;
}
i++;
printf("Do you have more data to add? (1=Y, 0=N)\n");
scanf("%d", &DataToAdd);
}
sql = "DROP TABLE IF EXISTS MODBUS";
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if( rc ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Table dropped successfully or was not present\n");
}
sql1 = "CREATE TABLE MODBUS(ID INT NOT NULL, REG INT NOT NULL, DATA INT NOT NULL);";
rc = sqlite3_exec(db, sql1, NULL, NULL, NULL);
if( rc ){
fprintf(stdout, "Table not created\n");
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Table created successfully\n");
}
for (j=0; j<i; j++)
{
sprintf(sql2, "INSERT INTO MODBUS(ID, REG, DATA) VALUES(%d, %d, %d);", TempID[j], TempReg[j], TempData[j]);
rc = sqlite3_exec(db, sql2, NULL, NULL, NULL);
if( rc ){
sprintf(message, "Item %d could not be added\n", (j+1));
fprintf(stdout, message);
fprintf(stderr, "SQL error: %s\n", zErrMsg);
break;
sqlite3_free(zErrMsg);
}else{
sprintf(message, "Item %d added successfully\n", (j+1));
fprintf(stdout, message);
}
}
sqlite3_close(db);
return 0;
}
然后我手动将构建输出可执行文件复制到raspberry Pi,并通过SSH将目录更改为其位置并尝试运行它。这是发生错误的地方。我有一个先前的错误,这是由于缺乏对可执行文件的权限,我已经对此文件做了什么修复它以前没有用,我也更改了数据库文件的权限。这次可能导致错误的任何想法?
编辑::
哦,应该说,我试图交叉调试但是“分段错误”错误导致它在我看到任何事情发生之前终止。