PurityLake
PurityLake

Reputation: 1120

MinGW g++ `strnlen` was not declared in this scope

currently I'm trying to run some code that I received from lynda.com but it is spitting out an error. The file I'm trying to compile is as follows, only including the calls to strnlen (strc.cpp):

#include <cstdio>
#include <cstring>
#include <memory>

#include "strc.h"

...

strc::strc() : data(nullptr) {
    msg("default ctor");
}

strc::strc(const char * s) {
    size_t slen = strnlen(s, _maxlen);
    data = new char[slen + 1];
    data[slen] = 0;
    memcpy(data, s, slen);
    msg("cstring ctor");
}

strc::strc(const strc & f) {
    size_t slen = strnlen(f.data, _maxlen);
    data = new char[slen + 1];
    data[slen] = 0;
    memcpy(data, f.data, slen);
    msg("copy ctor");
}
...

To compile this I am running:

g++ -c strc.cpp -o strc.o -std=c++11

I've tried replacing cstring with string.h and I've even tried but std:: in front of strnlen. Any help in this matter would be greately appreciated

Upvotes: 0

Views: 3889

Answers (5)

pfischer1687
pfischer1687

Reputation: 31

I believe the compiler error is happening because of the command-line flag -std=c++11. The C++ version of the header <cstring> appears to only have the following related function according to this cplusplus reference: std::size_t strlen( const char* str ). According to this Microsoft reference, the function strnlen, defined in the C version of the header <string.h>, may be more secure than strlen since it "calculates the length but doesn't walk past the end of the buffer if the string is unterminated". When I ran this code on GCC 11, it compiled successfully when I changed the command-line flag to -std=gnu++14 (or later, or even got rid of the flag) which enables the GNU extensions that include the C version of <string.h> (you can read more about GNU C extensions at this GNU reference). I encountered this same problem while trying to run some code that I received from lynda.com, so you may find the following Makefile helpful that I used to successfully compile a file unique.cpp that includes the header strc.h:

# Unique Pointer Makefile
CXX      = g++
CXXFLAGS = -O3 -funroll-loops -ftree-vectorize -Wall -std=gnu++17 
TARGET   = uni
OBJS     = strc.o unique.o

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $<

$(TARGET): $(OBJS)
    $(CXX) -o $@ $(OBJS)

.PHONY: clean
clean:
    rm -f $(TARGET) *.o

Upvotes: 0

ARibeiro
ARibeiro

Reputation: 114

I have a similar issue... I put this function there

static size_t strnlen(const char* s,size_t maxlen) {
    for (size_t i=0; i < maxlen ; i++ ){
        if (s[i] == '\0')
            return i;
    }
    return maxlen;
}

Upvotes: 1

Tu Nguyen
Tu Nguyen

Reputation: 27

You can replace strnlen by following snippet of code
strlen (s) < maxlen ? strlen (s) : maxlen

you can refer at strnlen

Upvotes: -2

dmi
dmi

Reputation: 1479

The correct header for strlen is:

#include <string.h>

Reference: strnlen

Upvotes: 0

M.M
M.M

Reputation: 141648

strnlen is not a Standard C++ function. I guess you've previously been using some other compiler which provided this as a non-standard extension.

You will need to change your code to either not call this function; or provide your own function with similar functionality (not calling it strnlen).

Upvotes: 4

Related Questions