为什么这个cmake不编译?

发布于 2025-02-06 19:33:09 字数 3232 浏览 0 评论 0 原文

我陷入了一个未编译的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上。

I'm stuck in a CMake project that isn't compiled. 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. The teste is very simple: the main calls a function that is in lib.
The compiler is generating .c.o compiled files instead of .o files. CMake generates the Makefile but when I run make it compiles ok but on link phase it gives me:

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:

#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:

#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 from ./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)

I am on a VM Oracle VirtualBox runing Ubuntu.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2025-02-13 19:33:09

您需要告诉 testecompila 使用 libteste 。尝试类似 target_link_libraries($ {local_project_name} public libtests)之类的东西。

You need to tell testecompila that is uses libteste. Try something like target_link_libraries(${LOCAL_PROJECT_NAME} PUBLIC libtests).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文