error: (1098) conflicting declarations for variable "_sprintf" in XC8

error: (1098) conflicting declarations for variable "_sprintf" in XC8

我想使用 sprintf 将字符串消息的长度(整数)附加到另一个字符串。但是每次我收到上述错误。

void ClientToServer(const char *client_msg)
{
    char str_len[20];
    int len = strlen(client_msg);
    char *ptr;
    sprintf(ptr,"AT+CIPSEND=0,%d",len);

}

完全错误:

CLEAN SUCCESSFUL (total time: 77ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
make  -f nbproject/Makefile-default.mk dist/default/production/TokenOverWiFi.production.hex
make[2]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/TokMain.p1 TokMain.c 
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/TokenTimer.p1 TokenTimer.c 
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/TokenWifi.p1 TokenWifi.c 
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/WifiCom.p1 WifiCom.c 
WifiCom.c:24:1: warning: (361) function declared implicit int
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -Wl,-Map=dist/default/production/TokenOverWiFi.production.map  -DXPRJ_default=default  -Wl,--defsym=__MPLAB_BUILD=1  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto      -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/TokenOverWiFi.production.elf  build/default/production/TokMain.p1 build/default/production/TokenTimer.p1 build/default/production/TokenWifi.p1 build/default/production/WifiCom.p1     
C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\sprintf.c:13:: error: (1098) conflicting declarations for variable "_sprintf" (WifiCom.c:24)
(908) exit status = 1
make[2]: *** [dist/default/production/TokenOverWiFi.production.hex] Error 1
nbproject/Makefile-default.mk:186: recipe for target 'dist/default/production/TokenOverWiFi.production.hex' failed
make[2]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 7s)

编辑:警告

将 stdio.h 添加到所有头文件的顶部时,错误消失并出现了几个警告。

CLEAN SUCCESSFUL (total time: 49ms) make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf make[1]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi' make  -f nbproject/Makefile-default.mk dist/default/production/TokenOverWiFi.production.hex make[2]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi' "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" 
-mcpu=18F25K80 -c  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/TokMain.p1 TokMain.c  "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c 
-fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/TokenTimer.p1 TokenTimer.c  "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c 
-fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/TokenWifi.p1 TokenWifi.c  "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80 -c 
-fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o build/default/production/WifiCom.p1 WifiCom.c  "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F25K80
-Wl,-Map=dist/default/production/TokenOverWiFi.production.map  -DXPRJ_default=default  -Wl,--defsym=__MPLAB_BUILD=1  -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto      -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/TokenOverWiFi.production.elf  build/default/production/TokMain.p1 build/default/production/TokenTimer.p1 build/default/production/TokenWifi.p1 build/default/production/WifiCom.p1      WifiCom.c:25:: warning: (1257) local variable "_ptr" is used but never given a value C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:558:: warning: (1498) pointer (sprintf@sp) in expression may have no targets C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:1465:: warning: (1498) pointer (sprintf@sp) in expression may have no targets C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:1550:: warning: (1498) pointer (sprintf@sp) in expression may have no targets C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:1564:: warning: (1498) pointer (sprintf@sp) in expression may have no targets ::: warning: (1311) missing configuration setting for config word 0x300003; using default

Memory Summary:
    Program space        used   89Ah (  2202) of  8000h bytes   (  6.7%)
    Data space           used   229h (   553) of   E41h bytes   ( 15.2%)
    Configuration bits   used     7h (     7) of     7h words   (100.0%)
    EEPROM space         used     0h (     0) of   400h bytes   (  0.0%)
    ID Location space    used     8h (     8) of     8h bytes   (100.0%)
    Data stack space     used     0h (     0) of   B41h bytes   (  0.0%)

make[2]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi' make[1]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'

BUILD SUCCESSFUL (total time: 9s) Loading code from C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi/dist/default/production/TokenOverWiFi.production.hex... Loading completed

您正在将字符串打印到既未初始化也未在堆中分配内存的指针上。从日志的外观来看,您似乎正在将程序写入某种嵌入式系统,该系统可能没有通过虚拟内存进行任何内存保护,如果有,则可能会引发分段错误。

声明ptr为任意大小的字符数组。鉴于您的格式字符串,我会说 30 就足够了,但是,如果需要,您可以将其设置得更高

void ClientToServer(const char *client_msg)
{
    int len = strlen(client_msg);
    char ptr[30];
    sprintf(ptr,"AT+CIPSEND=0,%d",len);

}

在您的代码中,ptr 可能拥有一些随机值,并将您的值写入您系统上的某个地址,该地址甚至可能没有映射到您的 instruction/data 内存(尤其是 NULL)。我还删除了似乎没有任何作用的 str_len 数组。