2017-02-13 88 views
2

代碼:警告交叉編譯的C

#include <string.h> 
//Return copy of str string from index position and len length 
char *StrCpy2(char *str, unsigned short index, unsigned char len) 
{ 
    char *text; 
    text = (char *)malloc(sizeof(char)*(len+1)); 
    if (text == NULL) return text; 
    strncpy(text, str + index, len); 
    text[len] = '\0'; 
    return text; 
} 

控制檯輸出尊重這個文件,行:

Compiling file: String.c 
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffast-math -O0 -ffunction-sections -fdata-sections -Wall -Wstrict-prototypes -Wextra -std=gnu99 -g -ggdb3 -fverbose-asm -Wa,-ahlms=out/String.lst -DSTM32F40_41xxx -DUSE_STM324x7I_EVAL -MD -MP -MF out/String.d -I. -IBACnet/inc -IBACnet/inc/objects -Iinc -Ilib/drivers/inc -Ilib/eval -IUI/inc -IuIP/inc String.c -o out/String.o 
String.c: In function 'StrCpy2': 
String.c:39:2: warning: implicit declaration of function 'strncpy' [-Wimplicit-function-declaration] 
    strncpy(text, str + index, len); 
^
String.c:39:2: warning: incompatible implicit declaration of built-in function 'strncpy' 
String.c:39:2: note: include '<string.h>' or provide a declaration of 'strncpy' 

我感到失落,因爲有些東西從stm32f1移植到時出錯stm32f4和codesourcery toolchain到bleeding-edge-toolchain (this one)。正如你所看到的,我已經包含了。也許一些#定義不正確?也許在日食或系統中的某些路徑是錯誤的?

+0

聽起來好像'strncpy'沒有在另一個工具鏈的'string.h'中定義。您可以通過檢查'string.h'文件來輕鬆檢查。 –

+0

在您的IDE路徑下(通常位於名爲'「include」'的文件夾中)查找名爲'「string.h」'的所有文件。在每個文件中搜索'strncpy'。聽起來就像你在stm32f1項目中包含的那樣聲明瞭這個函數,但是你在stm32f4項目中包含的那個不會。最後,在您的stm32f4項目設置中,將您的'include'路徑更改爲與stm32f1項目設置中相同的路徑。 –

+0

你確定'string.h'中有'strncpy'聲明嗎?它應該在那裏,但也許你正在使用一些過時的工具鏈或其他東西。無論如何,你可以自己寫這個函數,這並不難。 – ForceBru

回答

1

我幾乎可以肯定,您的項目中有一個名爲String.h的文件。由於您在Windows上編譯該文件,但這並不足以注意到您的String.h和工具鏈的string.h之間存在差異,因此#include <string.h>行實際上包含您的String.h,而您最有可能沒有該聲明。

如果你在Linux上編譯它,它很可能會起作用,因爲文件名的大小寫很重要。對於Windows,除了以不同的方式命名文件(尤其是頭文件),沒有其他解決方案。就個人而言,我會建議每個模塊都有一個全局函數(頭+源),然後可以在函數之後命名模塊。所以在上面的情況下,你會有StrCpy2.hStrCpy2.c