对符号“_Z5getchv”的未定义引用(在c++中使用getch())

发布于 2025-01-18 13:22:37 字数 4226 浏览 0 评论 0原文

我正在使用提供conio.h,conio.c的设备。在我的C ++代码中,每当我调用getch()时,我知道这些错误,

/usr/bin/ld: CMakeFiles/hello.dir/main.cpp.o: undefined reference to symbol '_Z5getchv'
//usr/lib/libPhantomIOLib42.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/hello.dir/build.make:96: recipe for target 'hello' failed
make[2]: *** [hello] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/hello.dir/all' failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

知道conio不是标准的

#if defined(WIN32)
#include <windows.h>
#include <conio.h>
#else
#include "conio.h"
#include <string.h"
#endif

我 conio.h is

#ifndef __CONIO_H_
#define __CONIO_H_

#ifdef _cplusplus
extern "C" {
#endif // _cplusplus

int _kbhit();
int getch();

#ifdef _cplusplus
}
#endif // _cplusplus

#endif // __CONIO_H

and conio.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

static struct termios term_attribs, term_attribs_old;

static void restore_term(void)
{
    if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attribs_old) < 0)
    {
        perror("tcsetattr: ");
        exit(-1);
    }
}

int _kbhit()
{
    static int initialized;
    
    fd_set rfds;
    struct timeval tv;
    int retcode;
    
    if(!initialized)
    {
        if(tcgetattr(STDIN_FILENO, &term_attribs) < 0)
        {
            perror("tcgetattr: ");
            exit(-1);
        }

        term_attribs_old = term_attribs;

        if(atexit(restore_term))
        {
            perror("atexit: ");
            exit(-1);
        }

        term_attribs.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
        term_attribs.c_iflag &= ~(IXON | BRKINT | INPCK | ICRNL | ISTRIP);
        term_attribs.c_cflag &= ~(CSIZE | PARENB);
        term_attribs.c_cflag |= CS8;
        term_attribs.c_cc[VTIME] = 0;
        term_attribs.c_cc[VMIN] = 0;

        if(tcsetattr(STDIN_FILENO, TCSANOW, &term_attribs) < 0)
        {
            perror("tcsetattr: ");
            exit(-1);
        }

        initialized = 1;
    }   

    FD_ZERO(&rfds);
    FD_SET(STDIN_FILENO, &rfds);
    memset(&tv, 0, sizeof(tv));
    
    retcode = select(1, &rfds, NULL, NULL, &tv);
    if(retcode == -1 && errno == EINTR)
    {
        return 0;
    }
    else if(retcode < 0)
    {
        perror("select: ");
        exit(-1);
    }
    else if(FD_ISSET(STDIN_FILENO, &rfds))
    {
        return retcode;
    }
    
    return 0;
}

int getch()
{
    fd_set rfds;
    int retcode;

    FD_ZERO(&rfds);
    FD_SET(STDIN_FILENO, &rfds);
    
    retcode = select(1, &rfds, NULL, NULL, NULL);
    if(retcode == -1 && errno == EINTR)
    {
        return 0;
    }
    else if(retcode < 0)
    {
        perror("select: ");
        exit(-1);
    }

    return getchar();
}

我的cmakelists.txt

cmake_minimum_required(VERSION 3.10)
project(project2 VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_FLAGS "-W -O2 -DNDEBUG -Dlinux")
add_executable(hello main.cpp conio.c conio.h)
target_link_libraries(hello HDU HD rt ncurses stdc++ m)

来自conio。,我看到_cplusplus < /代码>应解决C ++代码。为什么有错误并修复它?我的操作系统是Ubuntu 18.04。公司提供的一个制品文件之一是

CXX=g++
CXXFLAGS+=-W -fexceptions -O2 -DNDEBUG -Dlinux -Iinclude
LIBS = -lHDU -lHD -lrt -lncurses -lstdc++ -lm

TARGET=ServoLoopDutyCycle
HDRS=include/StatsSampler.h
SRCS=src/ServoLoopDutyCycle.cpp \
     src/StatsSampler.cpp \
     src/conio.c
OBJS=$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(SRCS)))

.PHONY: all
all: $(TARGET)

$(TARGET): $(SRCS)
    $(CXX) $(CXXFLAGS) -o $@ $(SRCS) $(LIBS)

.PHONY: clean
clean:
    -rm -f $(OBJS) $(TARGET)

I'm working with a device that provides conio.h, conio.c. In my c++ code, whenever I call getch(), I get these errors

/usr/bin/ld: CMakeFiles/hello.dir/main.cpp.o: undefined reference to symbol '_Z5getchv'
//usr/lib/libPhantomIOLib42.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/hello.dir/build.make:96: recipe for target 'hello' failed
make[2]: *** [hello] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/hello.dir/all' failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

I know conio is not standard but it seems the company addresses the OS issue by

#if defined(WIN32)
#include <windows.h>
#include <conio.h>
#else
#include "conio.h"
#include <string.h"
#endif

where conio.h is

#ifndef __CONIO_H_
#define __CONIO_H_

#ifdef _cplusplus
extern "C" {
#endif // _cplusplus

int _kbhit();
int getch();

#ifdef _cplusplus
}
#endif // _cplusplus

#endif // __CONIO_H

and conio.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

static struct termios term_attribs, term_attribs_old;

static void restore_term(void)
{
    if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attribs_old) < 0)
    {
        perror("tcsetattr: ");
        exit(-1);
    }
}

int _kbhit()
{
    static int initialized;
    
    fd_set rfds;
    struct timeval tv;
    int retcode;
    
    if(!initialized)
    {
        if(tcgetattr(STDIN_FILENO, &term_attribs) < 0)
        {
            perror("tcgetattr: ");
            exit(-1);
        }

        term_attribs_old = term_attribs;

        if(atexit(restore_term))
        {
            perror("atexit: ");
            exit(-1);
        }

        term_attribs.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
        term_attribs.c_iflag &= ~(IXON | BRKINT | INPCK | ICRNL | ISTRIP);
        term_attribs.c_cflag &= ~(CSIZE | PARENB);
        term_attribs.c_cflag |= CS8;
        term_attribs.c_cc[VTIME] = 0;
        term_attribs.c_cc[VMIN] = 0;

        if(tcsetattr(STDIN_FILENO, TCSANOW, &term_attribs) < 0)
        {
            perror("tcsetattr: ");
            exit(-1);
        }

        initialized = 1;
    }   

    FD_ZERO(&rfds);
    FD_SET(STDIN_FILENO, &rfds);
    memset(&tv, 0, sizeof(tv));
    
    retcode = select(1, &rfds, NULL, NULL, &tv);
    if(retcode == -1 && errno == EINTR)
    {
        return 0;
    }
    else if(retcode < 0)
    {
        perror("select: ");
        exit(-1);
    }
    else if(FD_ISSET(STDIN_FILENO, &rfds))
    {
        return retcode;
    }
    
    return 0;
}

int getch()
{
    fd_set rfds;
    int retcode;

    FD_ZERO(&rfds);
    FD_SET(STDIN_FILENO, &rfds);
    
    retcode = select(1, &rfds, NULL, NULL, NULL);
    if(retcode == -1 && errno == EINTR)
    {
        return 0;
    }
    else if(retcode < 0)
    {
        perror("select: ");
        exit(-1);
    }

    return getchar();
}

My CMakeLists.txt is

cmake_minimum_required(VERSION 3.10)
project(project2 VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_FLAGS "-W -O2 -DNDEBUG -Dlinux")
add_executable(hello main.cpp conio.c conio.h)
target_link_libraries(hello HDU HD rt ncurses stdc++ m)

From the conio., I see _cplusplus which should address the c++ code. Why there an error and to fix it? My OS is ubuntu 18.04. One of the Makefiles provided by the company is

CXX=g++
CXXFLAGS+=-W -fexceptions -O2 -DNDEBUG -Dlinux -Iinclude
LIBS = -lHDU -lHD -lrt -lncurses -lstdc++ -lm

TARGET=ServoLoopDutyCycle
HDRS=include/StatsSampler.h
SRCS=src/ServoLoopDutyCycle.cpp \
     src/StatsSampler.cpp \
     src/conio.c
OBJS=$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(SRCS)))

.PHONY: all
all: $(TARGET)

$(TARGET): $(SRCS)
    $(CXX) $(CXXFLAGS) -o $@ $(SRCS) $(LIBS)

.PHONY: clean
clean:
    -rm -f $(OBJS) $(TARGET)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文