Reading files from same folder (c++ program) - c++

I have a program that draws the Earth and it uses the following code to read the texture file:
Images::RGBImage surfaceImage;
surfaceImage=Images::readImageFile("",Vrui::openFile("/home/rodrtu/Desktop/SolarSystem/land_shallow_topo_2048.png"));`
The way I have it set up it only works on my desktop, but I want other people to have access to my program files and pictures, and be able to get the program working on their computer. What should I use instead of using "/home/rodrtu/Desktop/SolarSystem/land_shallow_topo_2048.png"
If I add a folder to the same place as my .cpp file, should I make changes to my makefile?
Here is my makefile
VRUI_MAKEDIR := /opt/local/Vrui-2.6/share/make
ifdef DEBUG
VRUI_MAKEDIR := $(VRUI_MAKEDIR)/debug
endif
INSTALLDIR := $(shell pwd)
# Set resource directory: I added this images folder to the same place as my
# .cpp file, but it still doesn't work
RESOURCEDIR = images
########################################################################
########################################################################
# Include definitions for the system environment and system-provided
# packages
include $(VRUI_MAKEDIR)/SystemDefinitions
include $(VRUI_MAKEDIR)/Packages.System
include $(VRUI_MAKEDIR)/Configuration.Vrui
include $(VRUI_MAKEDIR)/Packages.Vrui
# Set installation directory structure:
BININSTALLDIR = $(INSTALLDIR)/$(EXEDIR)
RESOURCEINSTALLDIR = $(INSTALLDIR)/$(RESOURCEDIR)
########################################################################
########################################################################
PACKAGES = MYVRUI
########################################################################
########################################################################
ALL = $(EXEDIR)/NewPlanet
.PHONY: all
all: $(ALL)
########################################################################
#'make clean'
########################################################################
.PHONY: extraclean
extraclean:
.PHONY: extrasqueakyclean
extrasqueakyclean:
# Include basic makefile
include $(VRUI_MAKEDIR)/BasicMakefile
########################################################################
########################################################################
$(EXEDIR)/NewPlanet: $(OBJDIR)/NewPlanet.o $(OBJDIR)/drawShape.o

You should use relative path like Beta suggested. Put a "data" folder on the same folder than your executable and use :
Vrui::openFile("./data/land_shallow_topo_2048.png")

File opening should be relative to the program directory, so you could create a sub directory inside your source dir for pictures. Make sure to let the user know where to place these pictures however,
g-dev#g$ mkdir dat
g-dev#g$ mv pic.jpg dat/pic.jpg
then in source:
::readImageFile("", Vrui::openFile("pic.jpg")
adding the directory in CMake:
include_directories ("${PROJECT_SOURCE_DIR/dat}")
adding the directory in VS:
here
(make sure you're using provided MSVS macros for your file path $(ProjectDir) or $(SolutionDir) )

Related

How to use Sphinx in a Python project that depends on being built in place?

I have a large mostly Python project that I am trying to document. Part of this project is dependent on C++ source code which is made accessible via Cython.
When running the code like normal it runs fine, but in trying to do auto documenting with Sphinx I have run into issues. I think this guy had the right idea, but I cant make it work.
My Makefile looks like this
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
#$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
#$(SPHINXBUILD) -M $# "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
How can I tell Sphinx to run python setup.py build_ext --inplace and then reference the output .so file(s) before analyzing the code?
Thank you for your time.
You would want to add the step from the answer you linked as part of the Makefile generated from sphinx to before any sphinx specific command was executed.
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
#$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
#cd /path/to/setup.py; python setup.py build_ext --inplace
#$(SPHINXBUILD) -M $# "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Adding it above the catch-all command that sphinx generates means that the build command to generate cython code would be executed prior to the the sphinx related command.
I would suggest instead to structure the project directory to be more standard to what is used in python. Instead of having documentation be in the same directory as the source code instead have a separate directory for source code and documentation.
root directory/
myproject/ (source code for Cython module)
libs/ (generated .so files from Cython)
tests/ (directory to hold test cases that should run after building)
__init__.py
docs/
Makefile (from sphinx)
Makefile (project)
setup.py
The Makefile for the project would be responsible for building the source code and then building the documentation. Its Makefile would look similar to below:
.all:
#python setup.py build_ext
#cd tests; $(MAKE) python -m unittest
#cd docs; $(MAKE) html
The first part of all would generate the source code (and should be updated to place the .so files generated to the libs/ folder). The second part would go into the tests/ directory and run unittests (example with python's unittest library). The third part would go into the docs/ directory and run make for the target html using the Makefile generated by Sphinx. To do this you would also want to update the tests and docs to include libs/ in their path so that way they can import the .so files generated from the build. (Note: # symbol prevents the line from being output to the console. This should be omitted if it is desired to be seen as part of a build)

How can I convert a configure script to a CMakeLists file?

I'm wanting to use this library in my cmake project, however it comes with a configure file instead of CMakeLists.txt
#! /bin/sh
# waf configure wrapper
# based on http://code.google.com/p/waf/source/browse/configure
CUR_DIR=$PWD
#possible relative path
WORKINGDIR=`dirname $0`
cd $WORKINGDIR
#abs path
WORKINGDIR=`pwd`
cd $CUR_DIR
WAF=$WORKINGDIR/tools/waf
# Generates a Makefile. Requires that $WAF is set.
#
generateMakefile()
{
cat > Makefile << EOF
#!/usr/bin/make -f
# Waf Makefile wrapper
WAF_HOME=$CUR_DIR
all:
#$WAF build
all-debug:
#$WAF -v build
all-progress:
#$WAF -p build
install:
$WAF install --yes;
uninstall:
$WAF uninstall
clean:
#$WAF clean
distclean:
#$WAF distclean
#-rm -rf build
#-rm -f Makefile
check:
#$WAF check
dist:
#$WAF dist
.PHONY: clean dist distclean check uninstall install all
EOF
}
generateMakefile
"${WAF}" configure $*
exit $?
Are there automated tools for the conversion? Does CMake supoprt the use of configure files? Is there a rule of thumb for conversion - i.e. replace ... with add_library?
You don't need to convert an upstream library to cmake to be able to use it in cmake projects. As long as you're able to install and/or link to that library, you can configure your cmake project to use it.
A common pattern to consume third-party libraries with cmake is to use cmake's find_package() function by supplying your own special-purpose cmake module files to find and configure the library.
Say, you're hoping to load libfoo. Here are the steps:
create a directory within your project tree to store your custom cmake modules (say, ./cmake/modules)
in that directory create a text file named FindFoo.cmake.
Within FindFoo.cmake add checks to determine if foo is actually present in the system. If it is then set the relevant variables. Otherwise, throw an error. The ideal thing is to create a build target for that library, which you can simply add as dependencies to other cmake targets.
Configure your cmake project to use your local modules by adding set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/"),
Finally, configure your cmake project to include your Foo library by calling find_package(Foo REQUIRED).
The way to do the conversion is to read the configure script and understand what it does and how/why. Then write a CMakeLists.txt file that achieves the same.
There are no shortcuts.

compile all cpp files in directory with makefile

I am doing a project which is growing pretty fast and manually compiles the project is getting to much work. Therefore i want a makefile to automize this process. I makefile should only compile all the files from the src directory (and all those files in subfolders).
Project directory is as follows:
-src
--files.cpp
--files.h
--|-dirA
---|-aa.cpp
---|-aa.h
---|-ab.cpp
---|-ab.h
--|-dirB
---|-ba.cpp
---|-ba.h
---|-bb.cpp
---|-bb.h
and a main.cpp in the root directory
So far my makefile gets all the cpp files from the src folder:
SRC_DIR ?= ./src
OBJ_DIR := ./obj
SRC_FILES := $(shell find $(SRC_DIRS) -name '*.cpp')
I'm not sure how to continue from this.

Using cmake on windows without IDE

this might be a duplicate, but I've already spent a couple of hours searching for an answer... without solution. First of all I know this problem might not exist if I'd use a Linux, but I am on Windows.
I am pretty new to c++ but already got some experience with java and gradle. I try to use cmake just like I am used to use gradle. I already read the cmake wiki, but I either do not find the correct pages or I just don't understand it. Here is my directory structure:
MyProject
-bin
-include
--header1.h
--header2.h
--header3.h
--header4.h
--header5.h
--header6.h
-src
--CMakeLists.txt
--MyProjectConfig.h.in
--impl1.cpp
--impl2.cpp
--impl3.cpp
--impl4.cpp
--impl5.cpp
--impl6.cpp
-main.cpp
-CMakeLists.txt
My CMakeLists.txt in my project folder looks like:
cmake_minimum_required (VERSION 3.14)
project (MyProject)
add_subdirectory(src)
file(GLOB_RECURSE sources src/*.cpp include/*.h)
# The version number.
set (Tutorial_VERSION_MAJOR 0)
set (Tutorial_VERSION_MINOR 1)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/MyProjectConfig.h.in"
"${PROJECT_BINARY_DIR}/MyProjectConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find MyProjectConfig.h
include_directories("${PROJECT_BINARY_DIR}")
install (FILES "${PROJECT_BINARY_DIR}/MyProjectConfig.h"
DESTINATION include)
add_executable(MyProject main.cxx ${sources})
# add the install targets
install (TARGETS MyProject DESTINATION bin)
My CMakeLists.txt in the src folder looks like:
cmake_minimum_required (VERSION 3.14)
include_directories(${MyProject_SOURCE_DIR}/MyProject/include)
I use the command in the bin bin directory: cmake -G "MinGW Makefiles" -S ../src
I got 2 questions now:
(How do I tell cmake to always use MinGW? ( I don't want to use -G always)) solved
(The compiled file build\CMakeFiles\3.14.0-rc2\CompilerIdCXX\a.exe does not have the expected behavior. It should print "Hello world!" and "My Class", while "My Class" is printed from the attribute of a class created from impl1.cpp, however it does nothing.) needs clarification:
How do I build a windows .exe-file to ruin on the console?
Edit:
I have learned that I have to call cmake --build . in my bin directory after creating the cmake files. However I just don't get an exe-file. With flag -v I get this output:
"C:\Program Files\CMake\bin\cmake.exe" -SD:\git\MyProject\src -BD:\git\MyProject\bin --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\MyProject\bin\CMakeFiles D:\git\MyProject\bin\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'D:/git/MyProject/bin'
mingw32-make.exe[1]: Nothing to be done for 'all'.
mingw32-make.exe[1]: Leaving directory 'D:/git/MyProject/bin'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\MyProject\bin\CMakeFiles 0 ```
Using the -G option is the standard way of doing it. It prevents you from having to put system-specific settings into your CMake config (like the hardcoded paths to MingW) and lets you use other compilers without having to change build scripts.
The a.exe you started is not your build output. It should be called MyProjectExec.exe. Also, you need to specify all source files in your call to add_executable. add_subdirectory does not automatically add any source files (to what build output should it add them?), it just executes the CMakeLists.txt.

CMake splits the generated files between build and src directory

My problem is when I run $ cmake /path/to/source/ the resulting files and directories are split between the directory I'm calling from and /path/to/source/include.
Here is my CMake project:
File structure:
root:
|-CMakeLists.txt
|-src
| |-CMakeLists.txt
| |-"source files"
|-include # This is where part of the generated files are ending up.
| |-CMakeLists.txt
| |-"include files"
Here are my CMakeLists.txt's:
root/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(DUCKSIM)
# Add the root directory for the CMakeLists.txt being called. This is necessary for
# out-of-tree builds.
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# Add the directories containing source and header files.
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/include)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY $(pwd)) # This is an attempt to fix my problem
src/CMakeLists.txt:
# set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Set DUCKSIM_SOURCES as all .cpp files
file(GLOB DUCKSIM_SOURCES *.cpp)
# Set the name of the executable as "ducksim" and link it with main.cpp
# and every thing in the DUCKSIM_SOURCES variable.
add_executable(ducksim main.cpp ${DUCKSIM_SOURCES})
include/CMakeLists.txt:
# set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Set DUCKSIM_SOURCES as all .h files
file(GLOB DUCKSIM_SOURCES *.h)
Command add_subdirectory() will optionally accept a second path as a parameter, but that will indicate the binary directory, as indicated in the documentation. By giving your include folder as the second parameter, CMake assumes that you want the binaries to go there. That's why you end up with some files at the top level (CMakeCache.txt, etc.) and some files in the include folder.
For the record, using file(GLOB ...) is not recommended for collecting source files to compile. If you add a source file, no CMake file will have changed, and the build system won't regenerate.
Finally, you shouldn't need the file(GLOB ...) for the header files, but you probably need an include_directories() call for the include folder.