使用CMake编译xyano_prorate.c时遇链接及代码警告问题求助
编译xyano_prorate.c时的链接错误及编译警告问题解决
问题背景
我有一个名为xyano_prorate.c的程序,迁移后出现段错误,打算用SonarQube分析潜在编码问题,现在用CMake编译时遇到构建失败的情况。
我的CMakeLists.txt内容
message("------------------------------") message(" ... Building example ...") message("------------------------------") cmake_minimum_required(VERSION 3.16.3) # The name of the executable will be : "xyano_prorate_project" # When project(..) invoked the variable "PROJECT_NAME" will # have the value "xyano_prorate_project" project(xyano_prorate_project DESCRIPTION "Testing program" LANGUAGES C) # Set two new variables: one for src path and another for include path # (PROJECT_SOURCE_DIR is set automatically when project(...) invoked # its value is the path to the called CMakeLists.txt file) set(SRC_DIR ${PROJECT_SOURCE_DIR}/src) set(INC_DIR ${PROJECT_SOURCE_DIR}/headers) # Nexts adds the target (executable) name and its source files add_executable(${PROJECT_NAME} ${SRC_DIR}/xyano_prorate.c) # Specifies the include directories for the target target_include_directories(${PROJECT_NAME} PUBLIC "${INC_DIR}"/headers)
CMake配置输出
$ cmake -S .. -B ../build CMake Warning (dev) at CMakeLists.txt:23: Syntax Warning in cmake code at column 63 Argument not separated from preceding token by whitespace. This warning is for project developers. Use -Wno-dev to suppress it. ------------------------------ ... Building example ... ------------------------------ -- The C compiler identification is GNU 13.2.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Configuring done -- Generating done -- Build files have been written to: /builds/analytics/tam/xyanomi/target_dir/build
构建失败输出
$ /builds/group/analytics/mis/MIS-xyanomi/build-wrapper/build-wrapper-linux-x86-64 --out-dir "${BUILD_WRAPPER_OUT_DIR}" cmake --build build/ --config Release [ 50%] Building C object CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o xyano_prorate.pgc: In function 'strlwr': xyano_prorate.pgc:617:12: warning: implicit declaration of function 'tolower' [-Wimplicit-function-declaration] xyano_prorate.pgc:292:1: note: include '<ctype.h>' or provide a declaration of 'tolower' xyano_prorate.pgc: In function 'strupr': xyano_prorate.pgc:626:13: warning: implicit declaration of function 'toupper' [-Wimplicit-function-declaration] xyano_prorate.pgc:626:13: note: include '<ctype.h>' or provide a declaration of 'toupper' xyano_prorate.pgc: In function 'amountFC': xyano_prorate.pgc:2553:13: warning: 'return' with no value, in function returning non-void xyano_prorate.pgc:2509:8: note: declared here xyano_prorate.pgc: In function 'unloadtkt': xyano_prorate.pgc:11002:4: warning: implicit declaration of function 'getcwd'; did you mean 'getw'? [-Wimplicit-function-declaration] xyano_prorate.pgc:11013:4: warning: implicit declaration of function 'chdir' [-Wimplicit-function-declaration] xyano_prorate.pgc: In function 'unload_le_reste': xyano_prorate.pgc:11864:10: warning: type defaults to 'int' in declaration of 'BkgClass' [-Wimplicit-int] xyano_prorate.pgc:11865:5: warning: type defaults to 'int' in declaration of 'stpo' [-Wimplicit-int] xyano_prorate.pgc:11866:5: warning: type defaults to 'int' in declaration of 'operant' [-Wimplicit-int] xyano_prorate.pgc: In function 'main': xyano_prorate.pgc:13479:5: warning: 'return' with no value, in function returning non-void xyano_prorate.pgc:13093:5: note: declared here xyano_prorate.pgc: In function 'parcours_Hubs_Voyages': xyano_prorate.pgc:817:7: warning: '__builtin_memcpy' writing 6 bytes into a region of size 5 overflows the destination [-Wstringop-overflow=] xyano_prorate.pgc:634:41: note: destination object 'ArrTime1' of size 5 [100%] Linking C executable xyano_prorate_project /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o: in function `RollBackOrCommit': xyano_prorate.c:(.text+0xe): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x6f): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x8a): undefined reference to `ECPGget_sqlca'
更多未定义引用错误
xyano_prorate.c:(.text+0xe): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x6f): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x8a): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x93): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0xb3): undefined reference to `ECPGget_sqlca' /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o:xyano_prorate.c:(.text+0xd5): more undefined references to `ECPGget_sqlca' follow /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o: in function `RollBackOrCommit': xyano_prorate.c:(.text+0x140): undefined reference to `ECPGtrans' /usr/bin/ld: xyano_prorate.c:(.text+0x156): undefined reference to `ECPGtrans' /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o: in function `parcours_Hubs_Voyages': xyano_prorate.c:(.text+0x8ac): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x8b8): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0xa8f): undefined reference to `ECPGget_sqlca' /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o: in function `Enreg_in_Historique': xyano_prorate.c:(.text+0x140c): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x149d): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x1562): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x1618): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x1b98): undefined reference to `ECPGdo' /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o:xyano_prorate.c:(.text+0x1e14): more undefined references to `ECPGdo' follow /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o: in function `Voyage_et_Destination_eScan': xyano_prorate.c:(.text+0x1e20): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x1e93): undefined reference to `ECPGget_sqlca' /usr/bin/ld: CMakeFiles/xyano_prorate_project.dir/src/xyano_prorate.c.o: in function `Getpfm': xyano_prorate.c:(.text+0x48c6): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x48dc): undefined reference to `ECPGget_sqlca' /usr/bin/ld: xyano_prorate.c:(.text+0x4982): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x4a02): undefined reference to `ECPGdo' /usr/bin/ld: xyano_prorate.c:(.text+0x4a7a): undefined reference to `ECPGdo'
解决方案
1. 解决链接错误(未定义引用ECPG函数)
这些函数属于PostgreSQL的ECPG(Embedded C for PostgreSQL)库,需要在CMake中链接对应的库:
- 先确保系统安装了PostgreSQL开发包:
- Debian/Ubuntu:
sudo apt-get install postgresql-server-dev-all - RHEL/CentOS:
sudo yum install postgresql-devel
- Debian/Ubuntu:
- 修改CMakeLists.txt,添加库查找和链接逻辑:
# 查找PostgreSQL包 find_package(PostgreSQL REQUIRED) # 添加可执行文件 add_executable(${PROJECT_NAME} ${SRC_DIR}/xyano_prorate.c) # 链接PostgreSQL的ECPG库 target_link_libraries(${PROJECT_NAME} PRIVATE PostgreSQL::ECPG) # 指定包含目录 target_include_directories(${PROJECT_NAME} PUBLIC "${INC_DIR}" ${PostgreSQL_INCLUDE_DIRS})
2. 修复编译警告
- 隐式函数声明:在
xyano_prorate.c开头添加缺失的头文件:#include <ctype.h> // 解决tolower、toupper的隐式声明 #include <unistd.h> // 解决getcwd、chdir的隐式声明 - 无值返回:修改
amountFC和main函数,确保返回对应类型的值(比如return 0;)。 - 隐式int类型:给
unload_le_reste函数中的BkgClass、stpo、operant变量明确声明类型(根据实际业务需求选择char、int或自定义类型)。 - 内存拷贝溢出:检查
parcours_Hubs_Voyages函数中的memcpy调用,要么扩大ArrTime1的缓冲区大小,要么调整拷贝的字节数,避免越界。
3. 修复CMake的语法警告
原CMakeLists.txt中target_include_directories行存在路径重复问题,将"${INC_DIR}"/headers改为"${INC_DIR}":
target_include_directories(${PROJECT_NAME} PUBLIC "${INC_DIR}")
内容的提问来源于stack exchange,提问作者supertren
相关产品推荐
相关产品推荐

