gpt4 book ai didi

c++ - 调用 POCO c++ ServerSocket() 时 linux 上的段错误

转载 作者:太空狗 更新时间:2023-10-29 11:48:39 25 4
gpt4 key购买 nike

在安装了 POCO C++ 的 Fedora 29 上

dnf install poco-devel

这段代码:

#include <Poco/Net/ServerSocket.h>

int main() {
Poco::Net::ServerSocket svs(9980);
return 0;
}

使用此 Makefile 编译:

CC = g++
CPPFLAGS += -I/usr/include
CPPFLAGS += -I/usr/include/Poco
CFLAGS += -c
CFLAGS += -g
LDFLAGS += -g
LDLIBS += -lPocoFoundation
LDLIBS += -lPocoUtil
LDLIBS += -lPocoNet
SOURCES = test.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE = test

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@

.cpp.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@

install:
@echo "Build complete!"

在 gdb 中给出以下内容

 Thread #1 [test] 8015 [core: 3] (Suspended : Signal :
SIGSEGV:Segmentation fault) typeinfo name for
Poco::Net::Impl::IPv6SocketAddressImpl() at 0x4020c0
Poco::Net::SocketAddress::toString[abi:cxx11]() const at
0x7ffff7ce2239 Poco::Net::SocketImpl::bind() at 0x7ffff7ce74c8
Poco::Net::ServerSocket::ServerSocket() at 0x7ffff7ce0e78 main() at
test.cpp:6 0x4011c5

在 valgrind 中:

 ==8034== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==8034== Bad permissions for mapped region at address 0x4020C0
==8034== at 0x4020C0: ??? (in /path/to/test85-poco/test)
==8034== by 0x4BCB238: Poco::Net::SocketAddress::toString[abi:cxx11]() const (in
/usr/lib64/libPocoNet.so.60)
==8034== by 0x4BD04C7: Poco::Net::SocketImpl::bind(Poco::Net::SocketAddress const&, bool,
bool) (in /usr/lib64/libPocoNet.so.60)
==8034== by 0x4BC9E77: Poco::Net::ServerSocket::ServerSocket(unsigned short, int) (in
/usr/lib64/libPocoNet.so.60)
==8034==

将 -v 传递给 g++ 会产生:

g++ -I/usr/include -I/usr/include/Poco -c -g -v test.cpp -o test.o
Using built-in specs.
COLLECT_GCC=g++
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.2.1 20181105 (Red Hat 8.2.1-5) (GCC)
COLLECT_GCC_OPTIONS='-I' '/usr/include' '-I' '/usr/include/Poco' '-c' '-g' '-v' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -quiet -v -I /usr/include -I /usr/include/Poco -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase-strip test.o -g -version -o /tmp/ccDd0TCY.s
GNU C++14 (GCC) version 8.2.1 20181105 (Red Hat 8.2.1-5) (x86_64-redhat-linux)
compiled by GNU C version 8.2.1 20181105 (Red Hat 8.2.1-5), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/8/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include"
ignoring duplicate directory "/usr/include"
as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
/usr/include/Poco
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward
/usr/lib/gcc/x86_64-redhat-linux/8/include
/usr/local/include
/usr/include
End of search list.
GNU C++14 (GCC) version 8.2.1 20181105 (Red Hat 8.2.1-5) (x86_64-redhat-linux)
compiled by GNU C version 8.2.1 20181105 (Red Hat 8.2.1-5), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: a86aaae86aad006cc531272eca0396eb
COLLECT_GCC_OPTIONS='-I' '/usr/include' '-I' '/usr/include/Poco' '-c' '-g' '-v' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
as -v -I /usr/include -I /usr/include/Poco --64 -o test.o /tmp/ccDd0TCY.s
GNU assembler version 2.31.1 (x86_64-redhat-linux) using BFD version version 2.31.1-13.fc29
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/8/:/usr/libexec/gcc/x86_64-redhat-linux/8/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/8/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/8/:/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-I' '/usr/include' '-I' '/usr/include/Poco' '-c' '-g' '-v' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
g++ -g test.o -lPocoFoundation -lPocoUtil -lPocoNet -o test

更精细的方法会产生类似的结果:

#include <Poco/Net/ServerSocket.h>
#include <iostream>

int main() {
std::string Address("192.168.0.11:9980");
Poco::Net::SocketAddress addr(Address);

if (addr.family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv6)) {
std::cout << "Addr: ipv6 " << std::endl;
}
else if (addr.family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv4)) {
std::cout << "Addr: ipv4 " << std::endl;
}
else {
std::cout << "Addr: something else (err)." << std::endl;
}
Poco::Net::ServerSocket svs(addr);
return 0;
}

在控制台上:

 Addr: ipv4

gdb 堆栈跟踪:

 Thread #1 [test] 7865 [core: 0] (Suspended : Signal :
SIGSEGV:Segmentation fault) typeinfo name for
Poco::Net::Impl::IPv6SocketAddressImpl() at 0x403100
Poco::Net::SocketAddress::toString[abi:cxx11]() const at
0x7ffff7ce2239 Poco::Net::SocketImpl::bind() at 0x7ffff7ce74c8
Poco::Net::ServerSocket::ServerSocket() at 0x7ffff7ce0db7 main() at
test.cpp:39 0x40236a

和 valgrind 报告:

 ==7616== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==7616== Bad permissions for mapped region at address 0x403100
==7616== at 0x403100: ??? (in /path/to/test85-poco/test)
==7616== by 0x4BCB238: Poco::Net::SocketAddress::toString[abi:cxx11]() const (in
/usr/lib64/libPocoNet.so.60)
==7616== by 0x4BD04C7: Poco::Net::SocketImpl::bind(Poco::Net::SocketAddress const&, bool,
bool) (in /usr/lib64/libPocoNet.so.60)
==7616== by 0x4BC9DB6: Poco::Net::ServerSocket::ServerSocket(Poco::Net::SocketAddress const&,
int) (in /usr/lib64/libPocoNet.so.60)
==7616== by 0x402375: main (test.cpp:38)

问题:

如何使用 POCO 在 linux 上创建一个没有段错误的 ServerSocket?

最佳答案

直接从源代码构建 POCO 解决了这个问题。使用以下内容构建:

sudo dnf remove poco-devel
git clone https://github.com/pocoproject/poco
cd poco/build
cmake ..
make -j4
sudo make install

POCO 文件被写入/usr/local/include 和/usr/本地/库所以修改makefile如下:

CC = g++
CPPFLAGS += -I/usr/local/include
CPPFLAGS += -I/usr/local/include/Poco

CFLAGS += -c
CFLAGS += -g
LDFLAGS += -g
CFLAGS += -std=c++14
LDLIBS += -L/usr/local/lib
LDLIBS += -lPocoFoundation
LDLIBS += -lPocoUtil
LDLIBS += -lPocoNet
LDLIBS += -Wl,-rpath=/usr/local/lib
SOURCES = test.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE = test

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@

.cpp.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@

install:
@echo "Build complete!"

Linux 上使用 POCO 的 ServerSocket 现在可以在没有段错误的情况下执行。

关于c++ - 调用 POCO c++ ServerSocket() 时 linux 上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53569442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com