14

so far I have only used Linux to write C++ code, but I want to learn to do it in Windows as well using CMake to simplify things.

To get started, I have written the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)  
PROJECT( CMakeWin )

SET(CMAKE_BUILD_TYPE Release)

# find opencv and set variables
Set(OpenCV_DIR C:/Users/Erik/Documents/Libraries/opencv/build)
FIND_PACKAGE(OpenCV REQUIRED)


#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

ADD_EXECUTABLE( ${PROJECT_NAME}
src/main.cpp 
)


TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})

When I run CMake (using Visual Studio 11 configuration) it seems to work properly, but when I open the CMakeWin.sln project and build and then run it, I get

Unable to start program C:\..... The system cannot find the file 
specified.

But I have also received

access denied.

When I go directly to the Release or Debug folder and run

CMakeWin.exe

it runs as it shall. What can the problem be?

EDIT:

To avoid making visual studio trying to run ALL_BUILD, I had to set CMakeWin as the StartUp project. See https://simtk.org/forums/viewtopic.php?f=91&t=3676 and comments below.

1
  • Don't think here is something related to c++.
    – VP.
    Commented Aug 8, 2015 at 8:11

4 Answers 4

19

In Visual Studio, right-click your project and mark it as 'Set as Startup project'.

1
  • 3
    Thanks! This should be the accepted answer.
    – Avi
    Commented Jan 3, 2021 at 5:39
4

In Visual Studio, open up the project properties dialog (right click on the project in the solution explorer pane and choose properties). Go to Debug and see what the command line to run the app is. That is most likely what is wrong.

2
  • Thank you, I got it to work after some digging, but why was it looking for ALLWORK instead of CMakeWin.exe in the debug folder? Should I change anything in my CMakeLists.txt?
    – El_Loco
    Commented Aug 8, 2015 at 10:33
  • Alas, I've not used much CMake and never had problems when I have. My knowledge is more on the visual studio side. I suspect you will need to change something in the CMakeLists.txt file. Is there anything related to ALLWORK in your system (or in your environment variables)? Commented Aug 8, 2015 at 13:40
2

CMake uses the same configuration as the one you are used on Linux. The common structure for simple projects is to have the sources files, compiled files and executable in same directory. So you can compile and run from the same directory.

MSVC default configuration (I do not know how to change that, and I would not dare to) uses one folder per configuration. You can have as many as them as you want, common ones would be DebugUnicode, ReleaseUnicode, DebugAnsi and ReleaseAnsi.

The common structure is :

  • top level folder : solution
    • one folder per project in the solution (one solution can contain multiple projects) : source files
      • a res folder (optional) for resource files (icons, etc.)
      • a folder per configuration, Debug + Release : object and executable files

So it is by design that executable files are not in same folder as the solution. You can execute them from there by giving their relative path: Debug\project.exe

1

1.Close the solution. 2.Close Visual Studio. 3.Reopen your project.

This worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.