Skip to content

sketch cannot find strncpy_P because of header conflict #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
luc-github opened this issue May 27, 2015 · 10 comments
Closed

sketch cannot find strncpy_P because of header conflict #343

luc-github opened this issue May 27, 2015 · 10 comments

Comments

@luc-github
Copy link
Contributor

Hi I store my strings using PROGMEM to save some memory :
const char mystring[] PROGMEM = "my string!";

but when I want to access it to use it using strcpy_P
I have compilation error about missing strncpy_P
So I have added manually in my project :

char* ICACHE_FLASH_ATTR strncpy_P(char* dest, const char* src, size_t size) {
    const char* read = src;
    char* write = dest;
    char ch = '.';
    while (size > 0 && ch != '\0')
    {
        ch = pgm_read_byte(read++);
        *write++ = ch;
        size--;
    } 

    return dest;
}

and all is working.

But is that normal strncpy_P is missing ? or is there another way to read strings stored by PROGMEM?
If it is missing, is there any plan to add it ?
I have checked open/closed issues but nothing pop up sorry

Thanks

@Makuna
Copy link
Collaborator

Makuna commented May 28, 2015

In PgmSpace.cpp it already contains strncpy_P() so there should be no need for you to add it. There is something else wrong.

@luc-github
Copy link
Contributor Author

here the test sketch :

#include <arduino.h>
#include <PgmSpace.h>
const char mystring[] PROGMEM = "this is my string";
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
char mybuffer[50] ;
strcpy_P(mybuffer,mystring);
Serial.println(mybuffer);
delay(500);
}

and compilation error:


C:\Users\user\AppData\Local\Temp\build5883592928906091546.tmp\testme.cpp.o: In function `setup':
testme.cpp:(.text+0x28): undefined reference to `strncpy_P'
testme.cpp:(.text+0x3b): undefined reference to `strncpy_P'
collect2.exe: error: ld returned 1 exit status
Error compiling.

I am using the boardmanager version of ESP8266 package

@Makuna
Copy link
Collaborator

Makuna commented May 28, 2015

There seems to be header/path conflict.

If you remove the include for Arduino.h it will compile.
If you move the include for pgmspace.h above arduino.h, it will compile.

there should be no need to include prgmspace.h as arduino.h already does this for you.

@luc-github
Copy link
Contributor Author

actually before I did not use#include <PgmSpace.h>, just set#include <arduino.h>
I just added to see if it solved the problem but not

I did not tried change header position - just did now and yes it solve the problem
Thanks a lot

But is that a normal behaviour ?

@luc-github
Copy link
Contributor Author

I guess not as if I use only #include <arduino.h> I have compile error undefined reference to strncpy_P'`

@luc-github luc-github changed the title PROGMEM for strings missing strncpy_P to access them sketch cannot find strncpy_P because of header conflict May 28, 2015
@luc-github
Copy link
Contributor Author

I changed issue title to better match problem

@Makuna
Copy link
Collaborator

Makuna commented May 28, 2015

The issue is that extern "C" is incorrectly being applied inside Arduino.h to things that don't need it; in this case specifically the include for pgmspace.h as it is not a legacy "C" header file.

@Links2004
Copy link
Collaborator

fixed Links2004@936669e

@luc-github
Copy link
Contributor Author

Thanks a lot ^_^

igrr pushed a commit that referenced this issue Jun 2, 2015
add __attribute__ to printf functions for better compiler warning handling.
remove ICACHE_FLASH_ATTR, all cpp files are automatic in FLASH (ld script)
@ughodke
Copy link

ughodke commented Oct 27, 2015

FYI, downloaded and installed the EtherCard library from GitHub, and loaded up one of the sample app (SSDP).

When using Arduino IDE version 1.6.5 with Arduino Due board, compilation fails with the same "...strncpy_P..." error. Changing the order of the include files (suggested above) did not fix the problem. So switched to other boards like Leonardo, Uno, Ethernet etc, and the compilation was successful without any errors.

So I guess the error has something to do with board specific declarations, for the Due board or could be specific to this version of the Arduino IDE. I hope this helps someone having a similar issue.

igrr pushed a commit that referenced this issue Oct 29, 2015
add __attribute__ to printf functions for better compiler warning handling.
remove ICACHE_FLASH_ATTR, all cpp files are automatic in FLASH (ld script)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants