using openmp with a makefile and g++ - gcc

I am building a large project with a makefile that was originally built with icpc, and now I need to get it running with g++.
When it compiles the file that uses openmp, it uses the -c flag, and doesn't use any libraries, so it ends up being serial instead of openmp. All of the examples I am seeing aren't using this -c flag.
Is there some way to compile without linking, but using openmp?
edit:
I've been using the -lgomp flag(and the library is on the library path):
g++ -lgomp -c -w -O4 mainS.cpp
g++: -lgomp: linker input file unused because linking not done
Edit: my boss made several mistakes in the code, the makefile, and the documentation. Sorry to have wasted your time, at least it was less than the 5 hours I spend on it =/

Are you passing the flag to enable OpenMP (IIRC it's something like -fopenmp? If you don't chances are the compiler will ignore the OpenMP-related primitives and just produce serial code.
I don't think that -c (ie, compile only, don't like) has anything to do with your problem.

Perhaps the documentation helps...

Related

Use custom stdlib and libc with GCC

I am using GCC and I want to essentially read and load the stdlib/libc stuff from another location than /usr/include and /usr/lib. I tried to copy them to another place and compile it like this, but it doesn't work. I am not surprised that this naive approach didn't work, but it was worth a try.
gcc -nostdlib -nolibc -I<custompath>/include -L<custompath>/lib -xc test.c
Could someone nudge me in the right direction here?
With this command:
gcc -nostdlib -nolibc ...
you are asking GCC to not link with libc.
Of course it doesn't work (if your program is using libc functions). What did you expect?
Start by dropping these two flags. And if the result doesn't work then, tell us exactly what doesn't work (by editing your question).
See also documentation for the -sysroot option.

When i should use ld instead of gcc?

I want to know when i should use ld linker instead off gcc. I just wrote a simply hello world in c++, of course i include iostream library. If i want make a binary file with gcc i just use:
g++ hello hello.cpp
and i've got my binary file.
Later i try to use ld linker. To get object file i use:
g++ -c hello.cpp. Ok that was easy, but the link command was horrible long:
ld -o hello.out hello.o \
-L /usr/lib/gcc/x86_64-linux-gnu/4.8.4/ \
/usr/lib/gcc/x86_64-linux-gnu/4.8.4/crtbegin.o \
/usr/lib/gcc/x86_64-linux-gnu/4.8.4/crtend.o \
/usr/lib/x86_64-linux-gnu/crti.o \
/usr/lib/x86_64-linux-gnu/crtn.o \
/usr/lib/x86_64-linux-gnu/crt1.o \
-dynamic-linker /lib64/ld-linux-x86-64.so.2 -lstdc++ -lc
I know fact that gcc uses the ld.
Using gcc is better in all cases or just in most cases? Please, tell me somethink about cases where ld linker has advantage.
As you mentioned, gcc merely acts as a front-end to ld at link time; it passes all the linker directives (options, default/system libraries, etc..), and makes sure everything fits together nicely by taking care of all these toolchain-specific details for you.
I believe it's best to consider the GNU toolchain as a whole, tightly integrated environment (as anyone with an experience of building toolchains for some exotic embedded platforms with, say, dietlibc integration will probably agree).
Unless you have some very specific platform integration requirements, or have reasons not to use gcc, I can hardly think of any advantage of invoking ld directly for linking. Any extra linker-specific option you may require could easily be specified with the -Wl, prefix on the gcc command line (if not already available as a plain gcc option).
It is mostly a matter of taste: you would use ld directly when the command-lines are simpler than using gcc. That would be when you are just using the linker to manipulate a small number of shared objects, e.g., to create a shared library with few dependencies.
Because you can pass options to ld via the -Wl option, often people will recommend just using gcc to manage the command-line.

Disable optimizations for a specific file with autotools

I'm working on setting up autotools for a large code base that was once just a bash script compile and later just hand written Makefiles.
We have a set of files that require that compiler optimizations be turned off. These files are already in their own subdirectory, so they will have their own Makefile.am.
What's the proper way to drop any existing compiler optimizations and force a -O0 flag on the compiler for these specific files?
I went with Brett Hale's comment to use subpackages. I was able to insert
: ${CFLAGS="-O0"}
before AC_PROG_CC, which sets the appropriate optimization. The other solutions do not work, since the -g -O2 was getting added very last. You can never get another -O variable after it.
You don't have to remove existing optimizations: the last value of -O on the compiler invocation will be used, so it's good enough to just add -O0 at the end.
This is not directly supported by automake, but there's a trick you can use defined in the documentation.
Otherwise if you know you'll only ever invoke your makefile with GNU make you can play other tricks that are GNU make specific; you may have to disable automake warnings about non-portable content.

Using -flto with autotools

Given a C++ program that uses GNU autotools, what's the easiest way to compile it with -flto (link time optimization)? My understanding is that it is customary on Unix for such optimization flags to be specified by the user or packager, not by the programmer.
According to this post, the -flto flag needs to be passed as a compilation flag and as a linker flag, so:
./configure CXXFLAGS="-flto" LDFLAGS="-flto" ...
or possibly:
./configure CXXFLAGS="-flto" LDFLAGS="-Wc,-flto" ...
might work.

Tips on using GCC as a new user [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am really new to GCC and I don't know how to use it. I already have a copy of a pre-compiled gcc binaries I've downloaded from one of the mirror sites in the gcc website. Now, I don't know where to go from here. Please give me some tips on how I can proceed.
I am sorry for the rather vague question..
What I want are tips on how to use GCC. I've programmed in C in the past using the TC compiler. Thanks!
I really appreciate all of your suggestions. Thanks again.. :)
Use -Wall all the time.
Don't ignore warnings; fix them as they crop up.
Baby steps to start with.
Create the file you want to compile (hi.c) in your favorite editor, like:
#include <stdio.h>
int main (void) {
printf ("Hi there\n");
return 0;
}
Then from the command prompt, execute:
gcc -o hi hi.c
That will give you an executable you can then run.
Beyond that, it really depends on how much C (or C++ or other GCC language) you know. If you're a beginner at C, rather than just the GCC toolchain, get yourself a good beginner's book on the language and start reading. Most importantly, do the exercises, play with the code and so forth.
Based on your update that you're comfortable with C itself (the Borland line), you'll probably only be interested in the immediate GCC differences.
GCC is a command-line compiler. There are IDEs that use it but GCC itself is not an IDE. That means you'll probably be doing command-line compilation.
The basic forms of this are:
# creates an executable "exe" from your source file "src.c"
gcc -o exe src.c
# creates an executable "exe" from your source files "src.c" and "extra.c"
gcc -o exe src.c extra.c
# creates object files from your source files
gcc -c -o src.o src.c
gcc -c -o extra.o extra.c
# creates an executable file "exe" from your object files
gcc -o exe src.o extra.o
Once you get sick of doing that, you'll want to learn how to use make, a way of automating the build process with a file containing rules (dependencies and actions to take), such as:
all: exe
clean:
rm -rf exe src.o extra.o
rebuild: clean all
exe: src.o extra.o
gcc -o exe src.o extra.o
src.o: src.c
gcc -o src.o src.c
extra.o: extra.c
gcc -o extra.o extra.c
I don't do justice to the power of make here, it's far more expressive than it looks.
Unless you have no interest in portability, make sure you learn which features of GCC are GNU extensions to the standard. Granted, GCC is available on most machines, but it would usually be silly to restrict your code so it only compiles with GCC.
To that end, as well as the ubiquitous -Wall, I usually use -std=c99 (or -std=c89) with -pedantic. I like to work with -Wmissing-prototypes -Wstrict-prototypes to ensure that no functions are undeclared. Where the code is really clean, I will add -Wextra (more warnings than -Wall) and -Werror (treat warnings as errors). This makes sure that the code stays really clean - the compilation fails when there's a warning.
Makefiles are very helpful. You should definitely check out how to use them.

Resources