CMake 对 main 的未定义引用
当我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
int main (int argc, char *argv[])
(或其不带参数的等效项)必须出现在每个程序中。如果不查看源代码,很难说您的设置出了什么问题,但我有一种感觉,您尝试编译为可执行文件的每个文件中并不存在 main 函数,即opengl_02.cpp
、geometrics.cpp
或main.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
ormain.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, likeadd_executable(main main.cpp opengl_02.cpp geometrics.cpp)
. Hope it helps.