gpt4 book ai didi

c++ - 混合 C/C++ 代码中的结构指针

转载 作者:行者123 更新时间:2023-11-30 17:23:46 32 4
gpt4 key购买 nike

我在从 C++ 调用 C 函数时遇到问题。特别是,我有一个具有 C 结构成员变量的类的实例。我将指向该变量的指针传递给一个 C 函数,该函数是从成员函数调用的。我观察到变量指针地址在 C 函数内部和外部都没有改变。然而,结构体字段之一的地址实际上确实发生了变化。

代码概要:

extern "C"
{
#define ENAGLO
#include <rtklib.h>
}

class GNSS
{
protected: rtcm_t rtcm; // A C struct type in rtklib.h.
public: ~GNSS() { free_rtcm(&rtcm); }
public: GNSS() { init_rtcm(&rtcm); }
public: Load() {

// Add some stuff to the rtcm.obs element

// This is the problematic line. If one were to read the address
// of rtcm.nbyte inside and outside of the function below, its
// address is different! Thus, if the gen_rtcm3 function sets
// rtcm->nbyte = 3, it does not persist back into C++.
gen_rtcm3(&rtcm, 1001, 0);
}
}

rtklib.h 和源文件可在此处获取:RTKLIB

我在 Ubuntu 14.04 上使用 GCC 4.8.2。

更新:CMake 用于按以下方式构建 RTKLIB 库和 C++ 代码。我检查过,如果删除 -O3 似乎没有什么区别。

# Find all RTKLIB source files
set(RTKLIB_SOURCES
thirdparty/RTKLIB/src/ephemeris.c
thirdparty/RTKLIB/src/ppp.c
thirdparty/RTKLIB/src/preceph.c
thirdparty/RTKLIB/src/rcvraw.c
thirdparty/RTKLIB/src/rinex.c
thirdparty/RTKLIB/src/sbas.c
thirdparty/RTKLIB/src/rtkpos.c
thirdparty/RTKLIB/src/datum.c
thirdparty/RTKLIB/src/rtcm3.c
thirdparty/RTKLIB/src/options.c
thirdparty/RTKLIB/src/rtkcmn.c
thirdparty/RTKLIB/src/rtcm2.c
thirdparty/RTKLIB/src/stream.c
thirdparty/RTKLIB/src/ionex.c
thirdparty/RTKLIB/src/rcv
thirdparty/RTKLIB/src/rcv/binex.c
thirdparty/RTKLIB/src/rcv/rt17.c
thirdparty/RTKLIB/src/rcv/gw10.c
thirdparty/RTKLIB/src/rcv/nvs.c
thirdparty/RTKLIB/src/rcv/ublox.c
thirdparty/RTKLIB/src/rcv/septentrio.c
thirdparty/RTKLIB/src/rcv/ss2.c
thirdparty/RTKLIB/src/rcv/rcvlex.c
thirdparty/RTKLIB/src/rcv/novatel.c
thirdparty/RTKLIB/src/rcv/javad.c
thirdparty/RTKLIB/src/rcv/skytraq.c
thirdparty/RTKLIB/src/rcv/crescent.c
thirdparty/RTKLIB/src/convkml.c
thirdparty/RTKLIB/src/pntpos.c
thirdparty/RTKLIB/src/rtcm.c
thirdparty/RTKLIB/src/ppp_ar.c
thirdparty/RTKLIB/src/geoid.c
thirdparty/RTKLIB/src/rtcm3e.c
thirdparty/RTKLIB/src/solution.c
thirdparty/RTKLIB/src/streamsvr.c
thirdparty/RTKLIB/src/rtklib.h
thirdparty/RTKLIB/src/rtksvr.c
thirdparty/RTKLIB/src/qzslex.c
thirdparty/RTKLIB/src/lambda.c
thirdparty/RTKLIB/src/tle.c
)

# Create a rtklib library
add_library(crates_gnss_rtklib ${RTKLIB_SOURCES})
target_link_libraries(crates_gnss_rtklib
${LAPACK_LIBRARIES}
${BLAS_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${MATH_LIBRARY}
${RT_LIBRARY}
)
SET_TARGET_PROPERTIES(crates_gnss_rtklib PROPERTIES COMPILE_FLAGS "-O3 -DTRACE -DENAGLO -DENAGAL -DENAQZS -DNFREQ=3")

# Crate a gazebo receiver
add_library(crates_gnss_plugin src/GNSS.cc)
target_link_libraries(crates_gnss_plugin
${catkin_LIBRARIES}
${GAZEBO_LIBRARIES}
crates_gnss_rtklib
)

# THIS IS THE LINE THAT WAS ULTIMATELY NEEDED!
#SET_TARGET_PROPERTIES(crates_gnss_plugin PROPERTIES COMPILE_FLAGS "-O3 -DTRACE -DENAGLO -DENAGAL -DENAQZS -DNFREQ=3")

最佳答案

我终于弄清楚这是一个编译标志问题。我已将 RTKLIB 编译到共享库中,该库具有与链接它的 C++ 代码不同的一组编译标志。这会导致不同的结构大小和运行时问题。

关于c++ - 混合 C/C++ 代码中的结构指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27417972/

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