including header file in a separate folder - c++

I created a class (say, myclass.h/cpp). I want to use the class from many different places. Therefore, I put those files in a folder (say, C:\cpp_include) and I want to include them from whatever folder my codes are. I have a code which uses the class (say, main.cpp). In main.cpp, I include myclass:
#include "myclass.h"
I compile using a .pro file and nmake. In the .pro file, I specify the folder as:
INCLUDEPATH += C:\cpp_include
When I compile the code using nmake, myclass.h is properly included, but myclass.cpp doesn't seem to be found by compiler.
When I specify myclass.cpp as one of the source files in .pro file:
SOURCES += main.cpp C:\cpp_include\myclass.cpp
The exe file is built correctly. But, I would like myclass.cpp file to be found automatically when myclass.h is included, i.e. without setting myclass.cpp as a source file. Would this be possible? It looks like that's what happens with classes from Qt and Qwt (e.g .h/cpp files in /src/ folder in Qt and Qwt). Am I missing somthing?
Thanks a lot!
Daisuke

A simple technique is to have build scripts (makefiles) in the cpp directories. Write a rule that traverses the directories, executing the build scripts. This one step in isolating functionality and also allows one to use libraries.

That's just not how it works. The .cpp is the file that matters, header files (.h) just get copied into the other .cpp files. Therefore you need to add the myclass.cpp to your sources for compiling. Or, if it's a library class, you could also compile it once into a static library (.lib) and just add that to your linker files. But you ultimately need to somehow include you implementation in the project where it's used.

Related

How to include .cpp and .hpp files of a library in my c++ project in visual studio

Apologies for a very basic question. I am trying to port from a CMake project into a visual studio project.
Basically I want to run the project from visual studio without using the CMake file. In the project I need to port, there are many folders and sub folders that contains many .cpp and .h files. These are included to the main cpp file as using #includes.
My Case
The library I want to include
and my main.cpp
For instance to make things simple assume I have a main file main.cpp and this file includes #include "Libpfs/colorspace/colorspace.h". The Libpfs is a folder and it has many sub folders one of which is colorspace folder and this has many .h and .cpp files. One .h file is colorspace.h that is included in main.cpp using the #include and the folder also has .cpp file i.e. colorspace.cpp.
My Attempt
My objective is include them to my project.
Now here is what I have tried
in Visual Studio Project->properties->C/C++ in Additional Include Directories I gave the path of the folder that contains Libpfs but this approach did not work and gave linker errors this might be because I have no lib files for the Libpfs (correct me if I am wrong). I only have .cpp files of the corresponding .h files. I presume the cause of error is the the .cpp files are not compiled yet.
My Question
How can I include the cpp file to my project as well (not the lib files since I dont have those).
Using this for a source, I find the CMakeLists.txt to contain nothing special.
FILE(GLOB COLORSPACE_H *.h)
FILE(GLOB COLORSPACE_HXX *.hxx)
FILE(GLOB COLORSPACE_CPP *.cpp)
SET(LIBPFS_H ${LIBPFS_H} ${COLORSPACE_H} ${COLORSPACE_HXX} PARENT_SCOPE)
SET(LIBPFS_CPP ${LIBPFS_CPP} ${COLORSPACE_CPP} PARENT_SCOPE)
So you can just add all the files to a VS C++ project. I would use some directory management, to separate these sources from your own.
But anyhow, in that case, you should include the sources by relative path. E.g.
#include "../../Libpfs/colorspace/colorspace.h"
Alternatively, you could put everything in a separate C++ library (static .lib or dynamic .dll). In that case you should but the binaries in in a bin path and add that as additional library directory (project properties of your own project) and put all the header files in an include/Libpfs path and add that as additional include directory. In that case you should include the files as.
#include <Libpfs/colorspace/colorspace.h>
On another topic
#define pow_F(a,b) (xexpf(b*xlogf(a)))
I found this define only in the sources of the same source used above.
It seems to be sourced from sleef and according to this it should give a speedup. But you should measure if that is really still true, instead of doing copy-paste/cargo cult programming.
I think generally you should use the standard library std::pow, which has overloads for float, double and long double. The compiler will in most cases optimize its use for you.
open explorer , look for C:\Program Files\Microsoft SDK\ and then go after dir with alot of .lib's in its \lib .
it's an msvc source for .lib and other stuff like that . copy your library there

CMake add_executable() use .cpp file or .hpp file?

I've read a bunch questions on this site about the usage of add_executable function in CMake, but have not found an exact answer to my confusion.
My question is why we only add .cpp files in the add_executable function but not .hpp files?
I understand header files are like "indices" for functions and classes in the corresponding .cpp files. But if we don't include them in the add_executable function, how are they used in the build process?
For example, will the A.hpp file be used when another source file import A.hpp? But then A.hpp is not in the add_executable function... How does the program know where to find A.hpp?
Thanks!
Header files, which often have .h or .hpp extension, although not always - for example, C++ standard library headers have no extensions - are "copy-pasted" by compiler into every .cpp (or .C, or .cc) which has #include directive to include the file.
Because of that, build system, such as CMake, doesn't have to know about them when the final executable is built - their contents is already accounted for by literal inclusion of their code into .cpp file.
However, build systems need to know about those files when dependencies are specified - to ensure that the whole application is rebuilt whenever any of those files is updated, and also to provide the proper inclusion path to the compilation command.

Compiling Eigen C++ QT

Eigen is located in the file
C:\Users\jherb_000\Downloads\eigen-eigen-07105f7124f9
I thought to include eigen you just use
`#include "C:\Users\jherb_000\Downloads\eigen-eigen-07105f7124f9\Eigen/Dense" `
But it is not compiling. I know it can work because I have done it before, and the eigen website does not explain how to do this unless you are using specific programs like g++.
Since you imply via the tags that you're using qt-creator, your problem boils down to "How do I add an include directory in qt-creator?" There are answers for that here, here and others. One thing to note is that the path you should add is C:\Users\jherb_000\Downloads\eigen-eigen-07105f7124f9.
What happens is when you include a specific file in a specific directory, if that file doesn't #include any other files (ok, other files that aren't in the include paths) all works well. But if it does, (and Eigen files include other file in the Eigen project) then the compiler does not know where to search for them. That's why you have to explicitly tell the compiler which directories to look for files that are included.
Very easy. Let's say you have a dependencies directory, and inside you have the eigen directory. In your .pro file, you could add your dependencies path to your INCLUDEPATH:
INCLUDEPATH += ../dependencies/ # or wherever that path is (relative to your .pro file)
Then, to include the Dense module, you do:
#include <eigen/Dense>
where eigen refers to your folder eigen in your dependencies folder. Many variations possible in function of your setup, but you got the idea.
Ok then what you need to do is:
Copy the C:\Users\jherb_000\Downloads\eigen-eigen-07105f7124f9\Eigen directory and all it's contents to wherever you're keeping all your third-party library files on your machine. (You probably wouldn't want to keep these files in your Downloads folder). For example let's say this directory is copied to C:\jacks_code\Eigen. Then,
Add this new directory to Qt-creator's list of directories to search (see Aki's answer for links):
In each of your source files, to include the Eigen templates, use the preprocessor directive:
#include <Dense>
The compiler will use the directories you told it, to dereference the file to C:\jacks_code\Eigen\Dense (the complete filename). It's a bit confusing here because the files in the root Eigen folder don't have .h or .c or .cpp or .hpp extensions.
Hope that helps. You can also read the INSTALL file in the base of the unzipped package.

Build makefile with source and header in separate folder

I'm trying to build a Makefile with separate folder for my sources and my headers. I have a the root of my project that contain an include folder that holds my .hpp files, and a source folder that holds my .cpp files. How can i build the Makefile that it builds all the .cpp with the respective .hpp files ?
How to create the program output in a folder called build ?
Thank you in advance
Make is so diverse, there's a LOT of ways to do this, but the end result is -I<your hpp path> (or -J) needs to be passed to the compiler to tell it additional include paths to search when resolving #include
The path needs to relative to the invocation of the rule directory, or relative to the file (I'm pretty sure the compiler searches both).
A lot of makefiles use CPPOPTS and CCOPTS or some variant of that in the makefile to pass extra options to the C or CPP compiler. Try adding:
CPPOPTS += -I..\include
To your makefile (assuming you've segregated your source and include files that way).
Again, this is ALL dependent on your makefile.
I'm trying to build a Makefile with separate folder for my sources and my headers.
An alternative way would be to have your headers in the same directory as your .cpp files. And in that top level include folder you can put symbolic links to the headers.

Adding header and .cpp files in a project built with cmake

I have built a project using cmake and some libraries.I want however to add some header and .cpp files in the project which I am going to code.What is the easiest way to do it?Can I just create a .cpp and header files and then build again project in Visual Studio? Or due to the fact that project was built using cmake I can't?
You can put all header/source files in the same folder and use something like
file(GLOB SOURCES
header-folder/*.h
source-folder/*.cpp
)
add_executable(yourProj ${SOURCES})
In this way, you can do either of the following two methods to add new added header/source into VS:
need to generate in CMake again.
fake to edit the CMakeLists.txt a little bit, e.g. simply add a space. And then build your solution in VS, it will automatically add new header/source files.
you need to add every .h and .cpp file to CMakeList.txt like this:
# Local header files here ONLY
SET(TARGET_H
Header.h
Plugin.h
messagelog.h
win32application.h
timer.h
)
# Local source files here
SET(TARGET_SRC
Plugin.cpp
messagelog.cpp
win32application.cpp
timer.cpp
)
then configure and build the solution again and reload it in VS.
Although it's a late Response and I just saw it. I am using CLion IDE from JetBrains, which adds these header and .cpp files automatically when you create them. Although it may not be your need, it may be useful for other peoples who see it.