CMake 对 main 的未定义引用

发布于 2024-12-22 16:46:08 字数 919 浏览 0 评论 0原文

当我尝试使用 make 编译程序时,我收到了对 main 错误的未定义引用。然而, main 存在于我的 src 目录中,我对自己做错了什么感到迷失。

我假设 add_executable([title] [source]) 是用于将源文件添加到编译的命令。

基于 cmake 教程

cmake_minimum_required(VERSION 2.6)
project(opengl_02)
add_executable(opengl_02 opengl_02.cpp)
add_executable(main main.cpp)
add_executable(geometrics geometrics.cpp)
set (opengl_02_version_major 1)
set (openfl_02_version_minor 0)

#configure the header file to pass some of the CMake settings 
#to the source code

configure_file(
    "${PROJECT_SOURCE_DIR}/opengl_02_config.h.in"
    "${PROJECT_BINARY_DIR}/opengl_02_config.h"
    )

#add the binary tree to the search path for include files
#so that it will find tutorialconfig.h

include_directories("{PROJECT_BINARY_DIR}")

add_executable(opengl_02_config opengl_02_config.cpp)

问题

为什么我的主文件没有被引用?

When I try to compile my program with make, I am getting an undefined reference to main error. Yet, main exists within my src directory, and I feel lost as to what I'm doing wrong.

I assume that add_executable([title] [source]) is the command used to add source files to the compilation.

Based on the cmake tutorial

cmake_minimum_required(VERSION 2.6)
project(opengl_02)
add_executable(opengl_02 opengl_02.cpp)
add_executable(main main.cpp)
add_executable(geometrics geometrics.cpp)
set (opengl_02_version_major 1)
set (openfl_02_version_minor 0)

#configure the header file to pass some of the CMake settings 
#to the source code

configure_file(
    "${PROJECT_SOURCE_DIR}/opengl_02_config.h.in"
    "${PROJECT_BINARY_DIR}/opengl_02_config.h"
    )

#add the binary tree to the search path for include files
#so that it will find tutorialconfig.h

include_directories("{PROJECT_BINARY_DIR}")

add_executable(opengl_02_config opengl_02_config.cpp)

Question

Why is my main file not getting referenced?

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

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

发布评论

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

评论(1

︶ ̄淡然 2024-12-29 16:46:08

int main (int argc, char *argv[])(或其不带参数的等效项)必须出现在每个程序中。如果不查看源代码,很难说您的设置出了什么问题,但我有一种感觉,您尝试编译为可执行文件的每个文件中并不存在 main 函数,即 opengl_02.cppgeometrics.cppmain.cpp。如果您确实想创建三个可执行文件,则 main 函数应该出现在示例中的所有三个源文件中。如果要从三个源文件创建可执行文件,则必须为单个可执行文件指定所有这些源文件,例如 add_executable(main main.cpp opengl_02.cpp Geometrics.cpp)。希望有帮助。

int main (int argc, char *argv[]) (or its equivalent without parameters) must be present in every program. It is hard to say what is wrong with your setup without looking at source code, but I have a feeling that main function is not present in every file that you are trying to compile into executable, i.e. opengl_02.cpp, geometrics.cpp or main.cpp. If you really want to create three executables, main function should be present in all three source files in your example. If you want to create executable from three source files, you have to specify all of them for a single executable, like add_executable(main main.cpp opengl_02.cpp geometrics.cpp). Hope it helps.

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