【问题标题】:CMake undefined reference to mainCMake对main的未定义引用
【发布时间】:2012-01-27 12:31:08
【问题描述】:

当我尝试使用make 编译我的程序时,我得到一个未定义的对主要错误的引用。然而,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)

问题

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

【问题讨论】:

    标签: c++ compilation cmake linker-errors


    【解决方案1】:

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

    【讨论】:

    • 最后一点是我需要的。谢谢,+1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2019-12-19
    相关资源
    最近更新 更多