0

I working on a C++ project, which I have almost a dozen template classes which naturally I have implemented then in .hpp files, but when I want to compile the project it takes almost a minute to compile and outputs a 7.8MB of the object file and 4.2MB of the execution file.

I know that the compiler has to instantiate every template objects but should it take this long? Does it not cache anything?

Wonder to know giving the circumstances how can I make the compilation faster??


P.S:
Working system: Linux 4.4.0-45-generic #66~14.04.1-Ubuntu GNU/Linux
G++ Version: g++ (Ubuntu 6.2.0-3ubuntu11~14.04) 6.2.0 20160901
Working C++ standard: C++14
Cmake Version: 3.6.2
Make Version: GNU Make 3.81


Edit: need to mention that I use boost <boost/multi_array.hpp> extensively in this project.

Cmake file:

cmake_minimum_required(VERSION 3.6)

SET(CMAKE_CXX_FLAGS "-std=c++14 -pthread")

project(THESIS)
set(CMAKE_BUILD_TYPE Debug)

file(GLOB_RECURSE ISOURCES "src/*.cpp")
file(GLOB_RECURSE IHEADER "inc/*.hpp" "inc/*.h" "lib_thesis/")

set (IINCLUDE_DIRS "")
foreach (_headerFile ${IHEADER})
    get_filename_component(_dir ${_headerFile} PATH)
    list (APPEND IINCLUDE_DIRS ${_dir})
endforeach()
list(REMOVE_DUPLICATES IINCLUDE_DIRS)

include_directories(${IINCLUDE_DIRS} "." "/usr/include/eigen3")

add_executable (thesis main.cpp ${ISOURCES})

target_link_libraries (thesis pthread boost_program_options)
dariush
  • 3,191
  • 3
  • 24
  • 43
  • 2
    Its difficult to know how long in *should* take without knowing the project size. Its possible your `Makefile` could be more efficient but without knowing what's inside it it is impossible to tell. It's also possible you could refactor the files in our project to make building faster but (again) its hard to gives specific advice without the specifics. One thing you can try is `ccache` which can give quite a boost in speed. – Galik Oct 28 '16 at 05:50
  • Berzerko templating can take a while. Made a humdinger of a mistake in a templated array generator once that took a while to generate. Surprised the compiler didn't crash. They make those things of stern stuff. – user4581301 Oct 28 '16 at 05:53
  • @Galik I have put my cmake file in the question, also the source code is available at [github](https://github.com/noise2/thesis/tree/sep/src), it just really a small project as you can see and shouldn't take this long. – dariush Oct 28 '16 at 05:54
  • Took a look at your stdafx.h fil to see if you were taking advangae of precompiled headers. Really don't suggest putting `using namespace std;` in there. Shouldn't slow down the build, but jinkies. One of the best hidden bug generators in the world. – user4581301 Oct 28 '16 at 05:58
  • And then I checked the compiler you're using. Weird to see stdafx.h in a file intended for gcc consumption. – user4581301 Oct 28 '16 at 06:03
  • @user4581301 ` Weird to see stdafx.h in a file intended for gcc consumption` what does that mean?? – dariush Oct 28 '16 at 06:07
  • stdafx.h is the standard name of the [MSVC precompiled headers file.](http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio) Nothing wrong with using the name, it's just I've never seen it outside of a visual studio project. – user4581301 Oct 28 '16 at 06:11
  • Which brings back to what I was looking for in the first place. You are using precompiled headers, yes? – user4581301 Oct 28 '16 at 06:17
  • @user4581301 that is because I put it there, I think this is a nice way to include the most commonly used stuff in project in every file. `You are using precompiled headers, yes?` I just include the `stdafx.hpp` in my files, nothing is precompiled here. – dariush Oct 28 '16 at 06:19
  • I see you're using the _Eigen template library_ which itself isn't a "small project". My guess is this is heavily influencing your compile time. – Ratatwisker Oct 28 '16 at 07:02
  • @Ratatwisker Yes since it was so heavy for my purpose I removed it from my project and used the boost::multi_array instead, I just forgot to remove it from my include path. – dariush Oct 28 '16 at 07:06
  • 1
    I try to avoid the common header trick because it blurs the actual requirements of a compilation unit. Plus every header added has a compile time cost. A cpp file that includes a lot of headers will take longer to compile. If you have a common header that includes headers that a file doesn't need, that's time wasted. Do it a lot, and that is a lot of time wasted. Another thing to look into is your antivirus solution and whether or not you are building over a network mount. `#pragma once` hates network mounts and symlinks. – user4581301 Oct 28 '16 at 07:15
  • @user4581301 Thanks for the taking trouble going through the codes, I have learned some new stuff today :) – dariush Oct 28 '16 at 11:47

0 Answers0