Linking gfortran libraries to c++ program in CodeBlocks under Ubuntu - linker

I have an Ubuntu under Vmware, and use Code::Blocks, as I am not a very powerful command line user
and prefer IDE to Vim+console.
I am trying to compile a program which uses a c++ wrapper to fortran library.
However compiling gives me the following errors:
undefined reference to `_gfortran_compare_string'
There are a lot of errors of this type and a bunch of other similar to this one.
I have gfortran 4.6.3. I found searching that it is probably a linking problem, and people say to
use -lgfortran option for linker. When I add this to linker options in the Code::Blocks it does not change anything, errors continue. So, here are some question:
Is this a correct option for linker?
May be I have to give linker a direct path to the fortran library?
How do I find where are the fortran libraries installed? (I don't know a lot about linux ((( )
What am I doing wrong and how to fix it.

Have you added gfortran as a library to linked to your project or lgfortran? The l is just an option for the g++ for linking the library gfortran to your code. I am not familar with Code::Blocks but you should look for a place where you can enter libraries you want to use and add the gfortran directly.
My guess is that the Code::Blocks side can help you to find this place.
Kim Kulling

My guess is something like /bin/lib /usr/lib or /usr/local/lib. Just take a look into your filesystem. Unfortunately I do not have a Linuix at work. Maybe someone else?
Kim Kulling

Related

MSP430 (msp430-gcc) linker undefined reference to

I'm trying to compile my MSP430 project using the Linux msp430-gcc compiler.
When I try to compile it using the CCS IDE it works just fine, but when I try to compile it using the msp430-gcc commandline tool, I get linker errors about functions like calloc and __no_operation.
The beginning of my source file looks like this:
#include <stdio.h>
#include <stdlib.h>
I compile the program using the following command:
msp430-gcc -mmcu=msp430g2553 -o test.out source_file.c
So it looks like I include the stdio.h and stdlib.h just fine, but I still get linker errors about functions like calloc.
I also tried using arguments like -lc, but that doesn't seem to help.
I read that both stdio and stdlib get included automatically by the compiler so I guess there is no need to use additional arguments for these files.
Does anyone have any idea how I could fix this?
Thanks.
mspgcc was a fork of gcc; by now, it's horribly outdated.
Nowadays, MSP430 development happens in gcc itself.
You could compile the latest version of gcc yourself, or hope that your distribution has a MSP430 cross compiler, or get it from TI.

difference of compiler between macOS and linux?

I am now learning c language, and my school put all assignments on myth, every time we have to log in by ssh and execute command remotely.
Thus I want to download the files and execute them on my own macbook. However when I use make command to compile the files, I got errors and warnings such as :
gcc -g -O0 -std=gnu99 -Wall $warnflags -m32 -c -I. vectest.c -o vectest.o
warning: unknown warning option '-Wlogical-op'; did you mean '-Wlong-long'?
vectest.c:10:10: fatal error: 'error.h' file not found
#include <error.h>”
I googled these problems but could not find a satisfactory answer. can anyone help me solve this ? or I have to use a linux machine instead?
Indeed; compilers for various platforms (even if it's the "same" compiler, such as GCC) may have different flags and behaviors. You may be able to get it to work - you could remove the -Wlogical-op flag from $warnflags in your Makefile, but if the error.h file is a system-supplied header file, you're probably in trouble. Therefore, I suggest that you download e.g. VirtualBox and run Linux on it.
See error(3) for what this header provides. It's not specific to linux but to the GNU C library. What you COULD do is provide your own minimal implementation of these functions and write your own error.h.
You could even `#define' them to do nothing at all, but then you will probably lose some error reporting in the existing code. Maybe you could try to find a teacher understanding the problem and discuss the issue ... it's probably better to learn standard C not using any platform-specific extensions.

after the installation via MacPorts of gcc45, how can I use it to build C language on my Mac?

The following html link contains all the relevant bash command line records of the installation process. Thank you for help!
That was a bad question
I didn't use XCode through I know Xcode will make it easier! I use an Air, memory of 4GB currently.
If this won't work easily I probably will quit learning C or run and compile C on Windows. :(
And XCode stuff, whatever.
You command-line output indicates rather clearly that you aren't telling gcc what to compile, so it's throwing its hands up in exasperation:
$ gcc
i686-apple-darwin10-gcc-4.2.1: no input files
You'll need to specify the file you're compiling. Better yet, use an IDE, like Xcode.
<Shrug> What do you want us to say?
Obviousy Macports is trying to build/install gcc but it can't without a compiler. Yes gcc can be built without a preexisting compiler, but good luck and why? Especially when XCode is a free download, click click let it start and a little while later it's done. At that point as pointed out elsewhere, gcc, g++ will work, but it's not actually gcc but clang in disguise.
If you want, you can use macports or brew or whatever later if you really want to, but again why? For programs that only work using gcc extensions? Doubt it. You just want a c/c++ compiler. If you ever want to do programs for the Mac or IPhone, you need XCode anyway, gcc won't do.

embedding lua code in c

I am attempting to follow the besic guide given here on embedding lua into C. I copied the code verbatim into my own embed.c file and executed the exact compiler command listed:
cc -o embed embed.c \
-I/usr/local/include \
-L/usr/local/lib \
-llua -llualib
I get the error:
embed.c:19:14: error: invalid storage class for function ‘openlualibs’
After which I moved the functions outside of main, compiled again, and got:
/usr/bin/ld: cannot find -llualib
I am at a loss for why I cannot compile this. lua is installed properly. has anyone else encountered these problems? If this is a bad tutorial, please feel free to simply direct me to a batter one.
On some Linux distributions you may need to install the lua-devel (or similar named) package, in order to get the proper header files and library symlinks required for compiling and linking projects against the package. If you do have a liblualib-<version>.so.<version>, for example liblualib-5.so.5.0, you may need to install the devel package.
Starting with lua 5.1, liblualib does not exist. Here is the release announcement: http://lua-users.org/lists/lua-l/2005-05/msg00186.html
I've had somewhat similar problems when embedding Lua. What I found that works for me is linking the dynamic link library (dl) and the math library (m). The math library may not be necessary if you're not using the lmath standard library.
cc -o embed embed.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl
This, of course, assumes that /usr/local/ is where the Lua files are installed, which is probably true.
As for the tutorial you linked to, I think it may be very out of date. Besides liblualib no longer existing, there are individual functions to open each standard library. These are the luaopen_* functions. Here's the relevant 5.1 reference manual entry. (I assume you're using 5.1, since that seems to be the version available in the packages) As for a better tutorial, I suggest the Programming in Lua book. Unfortunately, it was written for Lua 5.0. It is still mostly relevant, but I suggest you look over the relevant sections of the 5.1 reference manual, too.

How to trace or debug GMP?

I have downloaded the source of GMP library 5.02, and - as suggested here for maximum debuggability - I ran :
./configure --disable-shared --enable-assert --enable-alloca=debug --host=none CFLAGS=-g
and compiled it with make, then installed the library with make install. I then compiled my program like this: gcc -lgmp -std=c99 -g -c program.c and then I ran : ltrace ./a.out
However I realized that ltrace is not at all invoking the TRACE() functions I can find in the source code. I would like to trace the content that's in TRACE().
How should I go for that? Or is there any other straightforward way of debugging inside the GMP library? (I couldn't figure it out how to do it with gdb, it never wanted to step into gmp_printf)
Thanks.
EDIT:
I tried to investigate further, and realized that I couldn't modify the GMP library although I had the sources. I inserted a printf("hello\n"); at the very beginning of the mpz_init2 function which I do call at the beginning of my program, I recompiled all GMP (even after a make clean) re-installed the library with make install, then I compiled and launched my program, but it never printed "hello". I also made sure, I wasn't using another installed GMP library (when I do make uninstall my program cannot compile as it does not find the library). Still, I insisted that gcc looks for the library in the GMP source folder with the -L option.
I don't know what I'm doing wrong :(
Your final compile of a.out is not producing a statically linked a.out executable. So, even though as you state, during compilation of program.c the compiler is using your GMP library, at runtime it is picking up a shared library somewhere. You need to do one of two things:
Compile with -Bstatic (or something similar; check man page for your compiler)
Set the LD_LIBRARY_PATH (or something similar; check 'ld' or 'dyld' man pages)
I think #1 is actually your only choice given that you built only the static version of GMP. For #1 make sure you explicitly provide -L/path/to/gmplib in the compilation of program.c

Resources