Specify working directory with MSVC compiler - c++

So I have a project structure like this:
/
build/
data/
include/
src/
build.bat
My build.bat looks like this:
#echo off
pushd build
cl -Zi /EHsc ../src/*.cpp /I.. /Fepea1
It produces the executable, great. Now I have some data I have to read from data/ directory. Now the executable is in build/ directory, so I would have to copy it to the root directory or I would have to use relative path (like ../data). But I don't want to do that, cuz I don't know how will I be releasing this project (so I just want path = "data/). In Visual Studio there was a way to specify projects working directory. Can I do this with cl.exe? Or do I need to use relative paths?

You can just run the executable from the data folder.
..\build\out.exe and it will run from the data context.
You can do this using cmd.exe

Related

Project Linking and Compiling files

I want to start building a project and I have the following folder structure:
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
I have the MinGW compiler and I don't know how to compile all .cpp files. I know the command g++ *.cpp -o main for compiling all the files, but works only for files in the same folder.
Should I move all my files to the src folder? Should I change the project structure?
Also, I'm really doubtful if I should use CMake or not.
FINAL:
I decided to go with CMake which made my life easier.
For a barebones project, your structure is fine. Just add the following CMakeLists.txt file to the root of your directory:
cmake_minimum_required(VERSION 3.5)
# Given your project a descriptive name
project(cool_project)
# CHoose whatever standard you want here... 11, 14, 17, ...
set(CMAKE_CXX_STANDARD 14)
# The first entry is the name of the target (a.k.a. the executable that will be built)
# every entry after that should be the path to the cpp files that need to be built
add_executable(cool_exe src/main.cpp lib/class1.cpp)
# Tell the compiler where the header files are
target_link_libraries(cool_exe PRIVATE lib)
Your directory should now look like
CMakeLists.txt
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
Then to build the project, you will typically
Make a folder where you build everything (often called build, but it's up to you). Now the directory looks like
CMakeLists.txt
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
build
Go into the build folder and on the command like, configure your project with the command cmake .. (just to reiterate... this needs to be done from inside the build folder).
Build your project with the make command (again from inside the build folder).
After that, you should have an executable called cool_exe in the build folder.

MSBuild output directory

I'm trying to get absolute control over the output of MSBuild. If I run:
msbuild project.msbuild
or:
msbuild project.msbuild /p:configuration=Debug
I will get the folder:
Debug
If I run:
msbuild project.msbuild /p:configuration=Release
I will get the folder:
Release
The output directory structure I want is the following:
Win32\Debug
Win32\Release
x64\Debug
x64\Release
I first tried OutputPath but nothing happened. Then I tried the following:
<OutDir>$(Platform)\$(Configuration)\</OutDir>
Now I get the compiled binaries in the specified folder but for some reason the old directory structure is created to contain the object files. So if I run:
msbuild project.msbuild
What I end up with is:
Debug (contains object files)
Win32\Debug (contains binaries)
I want everything in the same folder, the one I specified, not the default MSBuild decides upon.
Object files are stored in 'Intermediate' directory.
By specifying OutDir - you specify location of binaries.
To specify location of .obj files you should additionally try specifying IntermediateOutputPath to the desired location of .obj files.
See reference, for example: https://blogs.msdn.microsoft.com/kirillosenkov/2015/04/04/using-a-common-intermediate-and-output-directory-for-your-solution/

How to build the project to specific folder with visual studio command line tools?

I've got this simplified folders structure for c++ project named project_name.
project_name
-build
-headers
-sources
-resources
If I do cl /EHsc /W4 sources/source1.cpp sources/source2.cpp /link /out:project_name ... it creates build files in current directory only which is project_name, resulting this:
project_name
-build
-headers
-sources
-resources
project_name.exe
source1.obj
source2.obj
etc
What I want is to specify the folder where all the build files have to be placed, in my case in a build folder.
What options are needed for cl or link to do that that from project_name directory ?
Place a .bat file in your project directory and use that to build your project.
pushd %~dp0/build
cl %~dp0/sources/source1.cpp %~dp0/sources/source2.cpp
popd
%~dp0 automatically expands to full path of your project directory so you can execute this .bat file from anywhere.

CMake file for a C++ project

There is some CMake magic I don't understand. How should a CMakeLists.txt file look like for a small C++ project with directories like this:
.
├── bin
└── src
├── src
└── test
bin — directory for built program
src/src — directory for source
src/test — directory for tests
The tests will need to include files from src/src.
I'd like to manage all the operations from cmake, however at this moment I even can't cause cmake to compile file in src/c.cpp.
Any help, links are welcome.
Your CMake files should reside in the main source directory and its sub-directories. The easiest approach is to have one CMakeLists.txt in the src directory, which includes all files from src/src and src/test. A very minimalistic example could look like the following:
# CMakeLists.txt in src
project(myExample)
set(myExample_SOURCES
src/file1.cpp
src/main.cpp)
add_executable(myExecutable ${myExample_SOURCES})
set(myExample_test_SOURCES
src/file1.cpp
test/test_file2.cpp
test/test_main.cpp)
add_executable(myTestSuite ${myExample_test_SOURCES})
The output directory is normally not specified, because you can have different active
builds in parallel with
different options, e.g. you can have one build in debug mode -O0 -g, another one in release mode with -O2 -g flags and a third one in release mode with heavy optimization flags -O3. Every build resides in its own directory (e.g. build-debug, build-rel,
build-opt).
You should create the output directory (bin in your case) manually and call the cmake command inside this directory. As an argument you have to supply the path to the main CMakeLists.txt. In other words, just execute
cmake ../src
when you are inside bin. This will take all files from the src directory and put the output to the bin directory.
You can easily create a second output directory, say bin2, where you specify different build flags. The ccmake provides a very minimalistic GUI for that.
This helped me to start with cmake examples.html

How do I tell cmake where to output its build data?

How do I tell cmake where it should output its build data?
Let's say I have a dir with the source code called src/,
and then since cmake outputs a lot of files I would like him to put all of that in
a dir called build/.
BUT I would like him to put the generated Makefile in the project root,
so I don't have to go into the build dir to build the application.
Is this possible with cmake?
I have managed to get the cmake out put if I fun cmake in the build dir like this:
cd build/
cmake ../src/
make
./hello
But it would be nice to stay in the project root and type something like this
cmake
make
./hello
I guess that I need to put a CMakeList.txt in the project root with some magic commands telling him where he could put the object files and where he can find the source code.
Thanks
Update:
Since my question is a little bit vague.
After I have run the cmake commands this is how I would like my tree to look like:
src/CMakeLists.txt
src/hello.c
src/hello.h
build/CMakeCache.txt
build/CMakeFiles/
build/cmake_install.cmake
CMakeLists.txt
Makefile
So the question is how should the CMakeLists.txt look like in this setup.
CMakeLists.txt
src/CMakeLists.txt
But maybe that is not possible?
BUT I would like him to put the generated Makefile in the project root, so I don't have to go into the build
dir to build the application.
cmake not designed for that, as I know,
BUT you can stay in the project root and type:
make -C build
./hello
with custom build rules or set_target_properties,
you can force cmake to put result executable to
sources directory or you can use
./build/hello
Type "cd build && cmake .." you need only once,
after that make will automaticaly start cmake, if something
changed.
cmake wants you to have a fresh build directory.
Okay, i get what you want. I think, you can achieve this with some machinery in CMakeLists.txt. Of course, it's not option if you are not project developer.
In root CMakeLists.txt you can add file(WRITE ...) command, which would write Makefile into ${CMAKE_SOURCE_DIR}. This Makefile would contain these commands for every target:
<target>:
cd ${CMAKE_BUILD_DIR} && ${CMAKE_MAKE_PROGRAM} <target>
So, now you can run make from source dir and it will build your project in build dir.
Simply use
cmake .
make
in your src directory. The (.) dot on unix systems addresses the current directory. Keep in mind doing so is actually not recommended since there will be a lot of build files in your src directory you'll have to clean up afterwards or at release time.