小编Cow*_*s42的帖子

C strndup隐式声明

我正在尝试使用strndup函数,但是我得到了错误

错误:函数'strndup'的隐式声明[-Werror = implicit-function-declaration]

我四处搜索,发现它不是标准函数,因此我必须使用不同的标志进行编译.但是,我通过以下编译收到同样的问题:

-std=gnu11
-Wall
-Wextra
-Werror
-Wmissing-declarations
-Wmissing-prototypes
-Werror-implicit-function-declaration
-Wreturn-type
-Wparentheses
-Wunused
-Wold-style-definition
-Wundef
-Wshadow
-Wstrict-prototypes
-Wswitch-default
-Wunreachable-code
-D_GNU_SOURCE
Run Code Online (Sandbox Code Playgroud)

我正在做一个任务,因此我必须使用所有这些,但我发现我必须使用'-D_GNU_SOURCE'进行编译才能使错误消失,但事实并非如此.

编辑:

我也包括这些:

#define __STDC_WANT_LIB_EXT2__ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
Run Code Online (Sandbox Code Playgroud)

非常感谢帮助解决这个问题.

c

4
推荐指数
1
解决办法
2173
查看次数

对'pthread_create'的未定义引用-pthread不起作用

我正在函数中创建新线程,并且我已经包含了pthread.h.但它不起作用,我在编译时不断收到以下错误:

对'pthread_create'的未定义引用

我用来编译的标志如下:

CFLAGS = -std = gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-声明-Wmissing的原型-Werror隐函数声明-Wreturn型-Wparentheses -Wunused -WOLD式清晰度-Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

编译器是gcc

Makefile文件:

CC=gcc
CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

all: finder

finder: stack.o list.o finder.o
    $(CC) -o mfind stack.o list.o mfind.o

stack.o: stack.c stack.h
    $(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
    $(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
    $(CC) -c finder.c $(CFLAGS)

clean:
    rm -f *.o finder
Run Code Online (Sandbox Code Playgroud)

c pthreads

2
推荐指数
1
解决办法
1568
查看次数

C Strcat valgrind错误

我正在尝试连接两个字符串,以便我可以获取文件路径.但是,我在valgrind中收到错误

条件跳转或移动取决于未初始化的值

我的代码:

/**
 * @brief Concatenate two strings to get file path
 * @param firstP - First string
 * @param secondP - Second string
 * @return Returns the concatenated string
 */
char *getPathDir(char *firstP, char *secondP) {
    char *new_str;
    int stringSize = strlen(firstP)+strlen(secondP)+2;

    if((new_str = malloc(stringSize)) != NULL){
        new_str[0] = '\0';
        strcat(new_str,firstP);
        new_str[strlen(firstP)] = '/';
        strcat(new_str,secondP);
    } else {
        perror("malloc");
        cleanUp();
        exit(EXIT_FAILURE);
    }
    return new_str;
}
Run Code Online (Sandbox Code Playgroud)

c string strcat

1
推荐指数
1
解决办法
414
查看次数

标签 统计

c ×3

pthreads ×1

strcat ×1

string ×1