[英]why this CMake don´t compile?
I'm stuck in a CMake project that isn't compiled.我被困在一个未编译的 CMake 项目中。 I isolated the problem in a simple test project.我在一个简单的测试项目中隔离了这个问题。 It is composed from a file teste
(main) and a lib that is in libs/
subdirectory.它由文件teste
(main) 和libs/
子目录中的 lib 组成。 The teste is very simple: the main calls a function that is in lib. teste 非常简单:main 调用 lib 中的函数。 The compiler is generating .co
compiled files instead of .o
files.编译器正在生成.co
编译文件而不是.o
文件。 CMake generates the Makefile but when I run make it compiles ok but on link phase it gives me: CMake 生成 Makefile 但是当我运行 make 它编译正常但在链接阶段它给了我:
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
Directory tree:目录树:
. ===> 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:测试.c:
#include "stdio.h"
#include "testelib.h"
int main(int argc, char** argv)
{
return testeint();
}
testelib.h: testelib.h:
int testeint();
testelib.c: 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来自 ./src 的 CMakeLists
####################
# 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 from ./libs/src来自 ./libs/src 的 CMakeLists.txt
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)
I am on a VM Oracle VirtualBox runing Ubuntu.我在运行 Ubuntu 的 VM Oracle VirtualBox 上。
You need to tell testecompila
that is uses libteste
.您需要告诉testecompila
使用libteste
。 Try something like target_link_libraries(${LOCAL_PROJECT_NAME} PUBLIC libtests)
.尝试类似target_link_libraries(${LOCAL_PROJECT_NAME} PUBLIC libtests)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.