multiple definition of `main'

文章目录

  • 1,目录结构
  • 2,报错内容
  • 3,cmake
  • 4,错误原因
  • 5,修改方案一
  • 5,修改方案二

1,目录结构

multiple definition of `main'_第1张图片

2,报错内容

multiple definition of `main'_第2张图片

3,cmake

cmake_minimum_required(VERSION 3.5)

project(app)

if(CMAKE_COMPILER_IS_GNUCC)
    message("COMPILER IS GNUCC")
    ADD_DEFINITIONS ( -std=c++11 )
endif(CMAKE_COMPILER_IS_GNUCC)

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -ggdb3")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
# 3. opencv
link_directories("/usr/local/lib")
include_directories("/usr/local/include/opencv4")

include_directories(${CMAKE_SOURCE_DIR})
# 6. 添加源文件
FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/*.cpp )
message(${CMAKE_SOURCE_DIR})
message(${SOURCE_FILES})
# 7. 添加链接库
LINK_LIBRARIES(opencv_core opencv_highgui)

# 9. 设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,
#但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(ALL_SRCS ${SOURCE_FILES})
message(${ALL_SRCS})

# 10.add executable file,添加要编译的可执行文件
ADD_EXECUTABLE(${PROJECT_NAME} ${ALL_SRCS})

4,错误原因

multiple definition of `main'_第3张图片

5,修改方案一

# 6. 添加源文件
#FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/*.cpp )
修改为
FILE(GLOB SOURCE_FILES ${CMAKE_SOURCE_DIR}/*.cpp )

5,修改方案二

multiple definition of `main'_第4张图片

# 6. 添加源文件
#FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/*.cpp )
修改为
FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.cpp )

你可能感兴趣的:(C++工程,cmake)