为什么这个cmake不编译?
我陷入了一个未编译的CMAKE项目。我在一个简单的测试项目中隔离了问题。它是由文件 teste
(main)和 libs/
子目录中的LIB组成的。测试非常简单:主调用lib中的函数。
编译器正在生成 .co
编译文件,而不是 .o
文件。 cmake生成了makefile,但是当我运行时,它可以编译好的,但是在链接阶段,它给了我:
make
Consolidate compiler generated dependencies of target testecompila
[ 50%] Linking C executable bin/testecompila
/usr/bin/ld: CMakeFiles/testecompila.dir/teste.c.o: na função "main": <=== .c.o file
teste.c:(.text+0x19): referência não definida para "testeint" <=== the libs fct I call in main
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/testecompila.dir/build.make:97: bin/testecompila] Erro 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/testecompila.dir/all] Erro 2
make: *** [Makefile:91: all] Erro 2
目录树:
. ===> testcompila dir
├── libs
│ ├── bin
│ │ └── Makefile ==>>> Generated from CMake
│ ├── include
│ │ └── testelib.h
│ └── src
│ ├── CMakeLists.txt
│ └── testelib.c
└── src
├── bin
│ └── Makefile ==>>> Generated from CMake
├── CMakeLists.txt
└── teste.c
test.c:
#include "stdio.h"
#include "testelib.h"
int main(int argc, char** argv)
{
return testeint();
}
testelib.h:
int testeint();
testelib.c:
#include "testelib.h"
int testeint()
{
int C = 0;
C++;
return C;
}
teste.c:teste.c:
#include "stdio.h"
#include "testelib.h"
int main(int argc, char** argv)
{
return testeint();
}
cmakelists from ./ src
####################
# Global #
####################
cmake_minimum_required(VERSION 3.12)
#set(CMAKE_CXX_STANDARD 17)
#####################
# Project #
#####################
# Project variables
set(LOCAL_PROJECT_NAME "testecompila")
set(LOCAL_PROJECT_VERSION "0.0.1")
set(LOCAL_PROJECT_DESCRIPTION "Teste compilação")
# Source files (relative to "src" directory)
set(SOURCES
teste.c
)
# Compiler definitions
set(DEFINES
)
# Compiler options
set(OPTIONS
)
# Project setup
project(${LOCAL_PROJECT_NAME}
VERSION ${LOCAL_PROJECT_VERSION}
DESCRIPTION ${LOCAL_PROJECT_DESCRIPTION}
LANGUAGES C)
add_executable(${LOCAL_PROJECT_NAME} teste.c)
list(TRANSFORM HEADERS PREPEND "../include/")
list(TRANSFORM SOURCES PREPEND "../src/")
INCLUDE_DIRECTORIES(../libs/include/)
message ("======>" ${LOCAL_PROJECT_NAME})
set_target_properties(${LOCAL_PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "bin")
set_target_properties(testecompila PROPERTIES LINKER_LANGUAGE C)
target_include_directories(testecompila PUBLIC ${PROJECT_BINARY_DIR})
####################
# Dependencies #
####################
cmakelists.txt。 libs/src
cmake_minimum_required(VERSION 3.23)
project(libteste)
add_library(libteste STATIC testelib.c)
SET_SOURCE_FILES_PROPERTIES(testelib.c PROPERTIES LANGUAGE C)
INCLUDE_DIRECTORIES(/usr/include )
set(CMAKE_EXE_LINKER_FLAGS --verbose)
我在VM Oracle VirtualBox Ubuntu上。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要告诉
testecompila
使用libteste
。尝试类似target_link_libraries($ {local_project_name} public libtests)
之类的东西。You need to tell
testecompila
that is useslibteste
. Try something liketarget_link_libraries(${LOCAL_PROJECT_NAME} PUBLIC libtests)
.