0

I found a useful code on GitHub and tried to build it by simply typing make command but without a success. The error was the following:

Makefile:1: Makefile.h: No such file or directory
make: *** No rule to make target `Makefile.h'.  Stop.

since I do not know what Makefile.h I tried the following CMake file:

cmake_minimum_required (VERSION 3.6.2)
project (JtTest)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(CMAKE_CXX_STANDARD 23)
else()
set(CMAKE_CXX_STANDARD 20)
endif()

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# using Clang
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# using GCC
add_definitions("-Wall -Wextra -Wno-unused-function -pedantic -pthread")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# using Intel C++
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# using Visual Studio C++
# add_compile_options("/std:c++latest")
add_compile_options("/W4" "/Zc:__cplusplus")
add_definitions(-MP -D_UNICODE)
endif()

find_package(Threads)

file(GLOB_RECURSE CPP_FILES ${CMAKE_SOURCE_DIR}/*.h ${CMAKE_SOURCE_DIR}/*.cpp)

add_executable(${PROJECT_NAME} ${CPP_FILES})

target_link_libraries (${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})

all the sources compiled successfully but not linked, because there are the following cpp files with main function in CMAKE_SOURCE_DIR directory:

test_cv.cpp
test_cvcb.cpp
test_cvprodcons.cpp
test_cvrace.cpp
test_cvrace_hh.cpp
test_cvrace_pred.cpp
test_cvrace_stop.cpp
test_jthread1.cpp
test_jthread2.cpp
test_stoken.cpp
test_stokencb.cpp
test_stokenrace.cpp
test_stopcb.cpp

what is the right way to compile them all with CMake?

Ideally I need a separate executable for each cpp file.

EDIT1

I am able to build the executable from a single source file if I change *.cpp with test_cv.cpp for example:

file(GLOB_RECURSE CPP_FILES ${CMAKE_SOURCE_DIR}/*.h ${CMAKE_SOURCE_DIR}/test_cv.cpp)

but with *.cpp I get the following linker errors:

duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace_pred.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_cvcb.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_cvprodcons.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace_hh.cpp.o
duplicate symbol 'testCVAnyMutex()' in:
    CMakeFiles/JtTest.dir/test_cvrace_hh.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace_stop.cpp.o
duplicate symbol '_cv' in:
    CMakeFiles/JtTest.dir/test_cvrace_hh.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace_stop.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace_stop.cpp.o
duplicate symbol '_m' in:
    CMakeFiles/JtTest.dir/test_cvrace_hh.cpp.o
    CMakeFiles/JtTest.dir/test_cvrace_stop.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_jthread1.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_jthread2.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_stoken.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_stokencb.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_stokenrace.cpp.o
duplicate symbol '_main' in:
    CMakeFiles/JtTest.dir/test_cv.cpp.o
    CMakeFiles/JtTest.dir/test_stopcb.cpp.o
ld: 16 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [JtTest] Error 1
make[1]: *** [CMakeFiles/JtTest.dir/all] Error 2
make: *** [all] Error 2
Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57
  • What do you mean by "not linked"? You don't need to link anything because all of the sources are in the created executable. You don't create any DLLs or LIBs. – Alon Adler Nov 21 '22 at 22:39
  • @AlonAdler I mean that I get linker errors, see EDIT1 – Alexey Starinsky Nov 21 '22 at 22:45
  • 1
    CMake will create as many executables as you have calls to `add_executable`. If you want to iterate over list of files, then use regular `foreach` function. – Tsyvarev Nov 21 '22 at 22:55
  • 3
    Yes, because you have more then one `main` - You'll have to call `add_executable` for each file. Adding to @Tsyvarev comment, look at this post about [Adding multiple executables in CMake](https://stackoverflow.com/questions/14306642/adding-multiple-executables-in-cmake) – Alon Adler Nov 21 '22 at 22:58

0 Answers0