0

I want to get two executables from my C++ source code (test and release). I have two main() functions in two separate C++ files.

With the Meson build system it is easy:

project('PrjName', 'cpp')

mainSrc = ['header1.hpp', 'source1.cpp', 'source2.cpp']
testSrc = ['header2.hpp', 'source2.cpp', 'test.cpp']

mainExe = executable('prjName', mainSrc)
testExe = executable('prjNameTest', testSrc)

I could not get the same with CMake:

cmake_minimum_required(VERSION 3.10)
project("PrjName")

set(SOURCES
    "header1.hpp"
    "source1.cpp"
    "source2.cpp"
)

set(TEST_SOURCES
    "header2.hpp"
    "source2.cpp"
    "test.cpp"
)

add_executable("prjName" ${SOURCES})
add_executable("prjNameTest" ${TEST_SOURCES})

I get the first executable (prjName), but not the second, with the error:

Multiple definition of 'main'

However, the main() functions are defined in "source1.cpp" and "test.cpp", so there should be no conflict.

How can I fix this issue, considering that as it seems from the Meson build the code should be fine?

Pietro
  • 12,086
  • 26
  • 100
  • 193
  • Show the code in `source1.cpp`, `source2.cpp` and `header1.hpp` in form of [mcve]. `I get the first executable (prjName), but not the second, with the error:` - Normally, before the first executable is completely built, the second one doesn't start to compile. If the problem doesn't depend on the second executable, remove it from the `CMakeLists.txt` and try again. – Tsyvarev Mar 12 '18 at 20:01
  • Put your real script here. It is obvious that the files in meson project are diferents from CMake projects – Amadeus Mar 12 '18 at 20:41
  • For instance, TEST_SOURCES has "source2.cpp" where it only has "source1.cpp" for the Meson system – Nibor Mar 12 '18 at 21:43
  • @Nibor: I fixed the typo. Thank you. – Pietro Mar 12 '18 at 21:58
  • What do you want from us? Your `CMakeLists.txt` **script is correct**. (Assuming you want to compile "source2.cpp" and "test.cpp", located in the source directory (near the `CMakeLists.txt`), into the second executable. It could be the problem with source files themselves, but you don't show them. – Tsyvarev Mar 12 '18 at 23:00

1 Answers1

1

Building the same code on a different PC, I had no such issue.

I would close/delete this question.

Pietro
  • 12,086
  • 26
  • 100
  • 193