2017-02-13 77 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