ozgeneral
ozgeneral

Reputation: 6789

Does gcc automatically use -j4? Is there anything I can do to optimize my compilation?

Hello I am a beginner with Linux platform therefore I am not familiar with the terminal commands.

I am writing an application on C++ and I expect it to consume a lot of processing power. So I want to make sure I am using all available cores on my device (it has 4 cores).

I am using the following to create an executable file:

gcc -o blink -l rt blink.c -l bcm2835

where bcm2835 is the library I use for I/O. So my question is, is this command is using all available cores or is there anything I can do to optimize it? I am willing to use everything available, to throw the kitchen sink if it will make this code run faster.

Upvotes: 0

Views: 473

Answers (2)

Bryan Koch
Bryan Koch

Reputation: 13

Since you're using C++, you have this nice-enough crossplatform-enough thread library integrated for you (>=C++11).

Just make sure to add -std=c++11 so that

gcc -o blink -l rt blink.c -l bcm2835 

becomes

gcc -std=c++11 -o blink -l rt blink.c -l bcm2835

Docs and basic examples at http://www.cplusplus.com/reference/thread/thread/
Nicer looking docs at http://en.cppreference.com/w/cpp/thread/thread

You still have to program what to thread on your own though.

Upvotes: 1

Doug Currie
Doug Currie

Reputation: 41200

The -j jobs option is for make not gcc

When used with make it will cause multiple "recipes" to be executed in parallel. In this context, your gcc line is one recipe.

AFTER QUESTION REVISION

If you want your code to use multiple cores, you will need to use threads or processes. Look into pthreads.

Upvotes: 2

Related Questions