CMake undefined reference to main-Collection of common programming errors
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?
-
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. -
int main() must NOT be present in every program. If you write tests with Gtest, there is only one “int main” in gtest. All other tests start with “Test()” and also create binaries.
I have build multiple tests which created a binary for each, but this “undefined reference to” problem is NOT related to it.
Originally posted 2013-11-09 21:26:33.