2

I can tolerate the first build. But every compiling is slow even I don't change any source code. This is what I want to solve.

This is my compiling option. I use visual studio code.

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-std=c++11", "-stdlib=libc++", "-O2", "-l", "ssl", "-l", "crypto", "-L", "/usr/local/opt/openssl/lib", "-L", "/usr/local/lib", 
        "-l", "secp256k1", "-l", "boost_filesystem", "-l", "boost_system", "-l", "boost_thread", "-l", "boost_program_options",
        "-I", "/usr/local/opt/openssl/include", "-o", "cp-tx-parser", "-g", "-D", "COPYRIGHT_HOLDERS", "-D", "MAC_OSX",
        "main.cpp", "rawtransaction.cpp", "univalue.cpp", "univalue_read.cpp", "amount.cpp", "utilstrencodings.cpp", "uint256.cpp", "ripemd160.cpp",
        "sha256.cpp", "sha512.cpp", "hmac_sha512.cpp", "hash.cpp", "pubkey.cpp", "transaction.cpp", "script.cpp", "core_read.cpp", "core_write.cpp", "base58.cpp",
        "standard.cpp", "interpreter.cpp", "script_error.cpp", "cleanse.cpp", "utilmoneystr.cpp", "sha1.cpp", "chainparams.cpp", "chainparamsbase.cpp",
        "merkle.cpp", "protocol.cpp", "netbase.cpp", "sync.cpp", "key.cpp", "arith_uint256.cpp", "pagelocker.cpp", "random.cpp", "util.cpp",
        "utiltime.cpp", "block.cpp"
    ],
    "showOutput": "always"
}

I doubt two things:

  1. g++ command compiles all of code every time.
  2. Boost might be the cause. Precompiled library can be helpful. (*)

I found before the make command can compile only updated files. I'm looking into it now.

(*)

How to speed up g++ compile time (when using a lot of templates)


Update1

what build system are you using? by xaxxon

Here it is. I'm using MacOS. Seems that gcc and g++ are same.

$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Update2

I just started to read the following book named "Autotools". I'm not familiar with the compiling terms like make or configure. Seems it's very long way to solve this issue.. but I will.

https://www.nostarch.com/autotools.htm


Update 3

I'm also planing to read about make like folowings on this weekends.

https://www.gnu.org/software/make/manual/make.pdf http://www.oreilly.com/openbook/make3/book/index.csp

and cmake as xaxxon recommended.


Update 4

Making a make file by myself is more difficult than I expected. I'm seeing another way. I use Visual Studio Code with c/c++ extention. Compiling is fine but very slow every time as I mentioned. However, seems vscode can output a makefile. I'm taking a look at the way.

How do I set up VSCode to compile C++ code?


Update 5

As a result of comparing the tools; pure make, autotools and cmake, I decided to use cmake. And I'm now struggling with adding libraries.

CMakeList.txt

cmake_minimum_required(VERSION 2.8)
include_directories(/usr/include /usr/local/include /usr/local/opt/openssl/include)
link_directories(/usr/local/lib /usr/local/opt/openssl/lib)
add_library(ssl crypto secp256k1 boost_filesystem boost_system boost_thread boost_program_options)
add_definitions(-DCOPYRIGHT_HOLDERS -DMAC_OSX)
add_definitions("-std=c++11") 
add_executable(cp-tx-parser
  main.cpp
  rawtransaction.cpp
  univalue.cpp
.
.
.
  utiltime.cpp
  block.cpp
)

Error Message

$ cmake .
CMake Error at CMakeLists.txt:4 (add_library):
  Cannot find source file:

    crypto

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

Update 6

Problem solved. I'm still not sure about CMake but the incremental compile works well. Thank you all.

cmake_minimum_required(VERSION 2.8)

add_definitions("-std=c++11") 

# BOOST
set(Boost_INCLUDE_DIR /usr/local/include)
set(Boost_LIBRARY_DIR /usr/local/lib)
find_package(Boost COMPONENTS system filesystem thread program_options REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

# OpenSSL
set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
set(OPENSSL_LIBRARY_DIR /usr/local/opt/openssl/lib)
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/include)
find_package(OPENSSL COMPONENTS crypto REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
link_directories(${OPENSSL_LIBRARY_DIR})

# secp256k1
set(Secp256k1_LIBRARY_DIR /usr/local/lib)

add_executable(cp-tx-parser
  main.cpp
  univalue.cpp
  .
  .
  .
  transaction.cpp
  amount.cpp
  univalue_write.cpp
)

target_link_libraries(cp-tx-parser ${OPENSSL_LIBRARIES})
target_link_libraries(cp-tx-parser ${Boost_LIBRARIES})
target_link_libraries(cp-tx-parser ${Secp256k1_LIBRARY_DIR}/libsecp256k1.a)
zono
  • 8,366
  • 21
  • 75
  • 113
  • You might want to check out [ccache](https://ccache.samba.org/), as it enables caching of compilation units across multiple compilation attempts. – Kevin Ushey Jun 10 '17 at 03:14
  • Thanks. I will take a look. – zono Jun 10 '17 at 03:25
  • what build system are you using? – xaxxon Jun 10 '17 at 05:35
  • @KevinUshey better to have something that doesn't even try to rebuild the TU if it hasn't changed.. I don't understand the point of ccache. – xaxxon Jun 10 '17 at 05:35
  • 5
    Check your antivirus. – zdf Jun 10 '17 at 06:07
  • 4
    Compilers are dumb beasts - by listing all files in a single argument list, you are telling the compiler to recompile all of them. Compilers are dumb beasts and, if you tell them to compile everything at once, that's exactly what they do. Look up "incremental build". That involves compiling every compilation unit individually, only recompiling those affected by a change, and then linking. – Peter Jun 10 '17 at 07:12
  • @Peter Thank you. I will look into about "incremental build" – zono Jun 10 '17 at 07:18
  • @ZDF I'm using an antivirus app. I will check it out. – zono Jun 10 '17 at 07:21
  • Generally you want to call `g++` with the `-c` option (along with all `-W`, `-O`, `-I`, `-D`, `-std`, ... flags) for each `.cpp` file separately. This compiles them to `.o` files. Then you call `g++` (with `-L`, `-l`, ... flags) on all the `*.o` files to link them into an executable. Now when you change a `.cpp` file, you only have to regenerate its `.o` file and link again. But I don't know how to configure this with whatever IDE you're using. – melpomene Jun 10 '17 at 07:23
  • I don't think antivirus is relevant. As Peter said, your compile command tells it to compile everything over every time. – melpomene Jun 10 '17 at 07:24
  • @melpomene Thanks, I will try it now. – zono Jun 10 '17 at 07:26
  • I tried to compile separately but it's difficult to resolve the dependency. Making a makefile might be a good way. I'm looking into it. – zono Jun 10 '17 at 07:39
  • 3
    don't use autotools. Use cmake. Trust me. – xaxxon Jun 10 '17 at 08:27
  • +1 for CMake or Makefile. Autotools is the big gun. Use separate dependencies for each compilation unit. Use ccache – sehe Jun 10 '17 at 09:43

2 Answers2

1

You can use Xmake, it's very fast and it has a builtin cache system, distributed compilation system.

And it also provide a builtin package manager. You can integrate many c++ dependencies easily and quickly.

ruki
  • 183
  • 6
0

As said by Peter:

Compilers are dumb beasts - by listing all files in a single argument list, you are telling the compiler to recompile all of them. Compilers are dumb beasts and, if you tell them to compile everything at once, that's exactly what they do. Look up "incremental build". That involves compiling every compilation unit individually, only recompiling those affected by a change, and then linking.

You want a build system, which (among other things), usually takes care of implementing incremental builds. Some build systems you could look into are Ninja and (if you are on Windows), Visual Studio. Make and autotools also work, but come from an older era. I don't know about autotools, but while make has improved and is improving, newer buildsystems like Ninja have the privilege of less historical baggage. Like people are saying in the comments, I recommend using cmake, which is a buildystem generator. You configure it, and it will generate configuration files for many acutal buildsystems including all the ones I listed above (except autotools). CMake is widely used in the C++ ecosystem, which comes with advantages of compatibility when using dependencies.

starball
  • 20,030
  • 7
  • 43
  • 238