mastier
mastier

Reputation: 954

CMake Why cannot use .exe for custom target

Here is the CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project("cmake_oneoneone")
add_executable(main src/main.cpp)
set(library_name liba CACHE string "This variable is the library name")
add_library(${library_name} STATIC src/liba.cpp)
target_link_libraries(main ${library_name})
include_directories(include)
add_custom_target(run_main
              COMMAND ./main
              DEPENDS main
              )

The output is:

~/workspace/cmake_test/build$ make run_main
[ 50%] Built target liba
[100%] Built target main 
Hello world!
[100%] Built target run_main

but if I change executable to .exe

cmake_minimum_required(VERSION 2.8)
project("cmake_oneoneone")
add_executable(main.exe src/main.cpp)
set(library_name liba CACHE string "This variable is the library name")
add_library(${library_name} STATIC src/liba.cpp)
target_link_libraries(main.exe ${library_name})
include_directories(include)
add_custom_target(run_main
              COMMAND ./main.exe
              DEPENDS main.exe
              )

~/workspace/cmake_training/build$ make run_main
make[3]: *** No rule to make target `../main.exe', needed by `CMakeFiles/run_main'.  Stop.
make[2]: *** [CMakeFiles/run_main.dir/all] Error 2
make[1]: *** [CMakeFiles/run_main.dir/rule] Error 2
make: *** [run_main] Error 2

I tried either Linux and Windows on both the CMake behaves the same.

Upvotes: 1

Views: 945

Answers (1)

user3657398
user3657398

Reputation:

If you check the documentation of the ADD_EXECUTABLE command, you can figure out that CMake automatically adds the extension .exe to your executable in case that you are generating it in a Windows environment. If you are in Linux, obviously generate a .exe has no sense:

Adds an executable target called to be built from the source files listed in the command invocation. The corresponds to the logical target name and must be globally unique within a project. The actual file name of the executable built is constructed based on conventions of the native platform (such as .exe or just ).

So you do not have to add the .exe at the end of the name of your executable.

Upvotes: 2

Related Questions