Linking after using f2c - c

I used f2c to translate a huge Fortran subroutine into C. The header says the following:
/* fourier.f -- translated by f2c (version 20090411).
You must link the resulting object file with libf2c:
on Microsoft Windows system, link with libf2c.lib;
on Linux or Unix systems, link with .../path/to/libf2c.a -lm
or, if you install libf2c.a in a standard place, with -lf2c -lm
-- in that order, at the end of the command line, as in
cc *.o -lf2c -lm
Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
http://www.netlib.org/f2c/libf2c.zip
*/
I am using ubuntu 10.04. How can I link the object file with libf2c?

You would have to install the libf2c2-dev package -- but as the f2c package already depends on it, all you may need is to add -lf2c to your Makefile.

Are you compiling the resulting C file with gcc? Then add "-lf2c -lm" to the gcc compile command.
Why not compile with a Fortran compiler, such as gfortran? It's easily available for Ubuntu.

By passing -lf2c -lm to the line which will create the executable from the objects. Which compiler are you using on Ubuntu? GCC?
gcc -c fourier.c -lf2c -lm
Could be as simple as that.

Well - no direct answer to your linking problems, but:
Since you're working with Linux: Why don't you compile you fortran code as is and link it directly with the C-code? GCC can do that. Converting the code is of course doable but it is by no way required.
Nils

Related

command line prompt command (want to understand what its doing)

I am doing the CS50 class, I have installed the cs50.h.
Based on the instructions I used the following command in terminal to compile my simple program and just want to make sure I understand everything im asking terminal to do.
Line is:
gcc -g hello.c -o hello -lcs50 -lm
I know the following*: gcc =
gcc = gnu compiler for C
-g = generate source-level debug information
Hello.c = name of the file we want to compile
-o = write output file
hello = our output file name
Can anyone tell me what -lcs50 and -lm are? My guess is that its calling on the library lcs50 in (-lcs50) but again this is a guess and would like to know for sure.
Everything works as it should with no issues
Thanks,
Mostly correct.
-o is not required to generate the output file, it's only needed to customize the name. (-o and the following name can only appear together).
-lcs50 means "link the library called cs50", not lcs50. It will try to find this file using several different name patterns, e.g. libcs50.so (on Linux), [lib]cs50.dll[.a] (on Windows), libcs50.a (on both), something else on Mac.
-lm links the standard math library, but I don't think you need to manually specify it on most modern GCC distributions.
Yes. For -lm, it's for the maths library, which is not linked by default. This is explained well at Why do you need an explicit `-lm` compiler option.

Compiling cmocka on windows

I'm trying to compile a simple unit test on my windows machine.
When I'm trying to compile my test I'm using the shared library flag.
gcc -c -L./bin/ -lcmocka .\Test.c .\src\some_module.c
gcc .\Test.o .\some_module.o -o main
But the second line throws this error:
undefined reference to `_cmocka_run_group_tests'
However, if I'm compiling using directly the cmocka.c file which I downloaded from their git it works fine:
gcc -c .\lib\cmocka.c .\Test.c .\src\some_module.c
gcc .\Test.o .\some_module.o .\cmocka.o
What am I doing wrong in the first compilation?
In addition, I would happy to understand the difference between the two compilations. Which one is the better practice?
Thank you
In order to compile your code, the compiler does not need to know where to look for the library. It's enough if the compiler "finds" the declarations of the functions which are usually in the header files provided by the library.
This step is done in the first line of your compilation procedure (maybe you need to specify the folder to the header files by adding -Ipath/to/headers/):
gcc -c .\Test.c .\src\some_module.c
The library itself is "combined" with your code during the linking step, which is done during your second compilation step. Here you need to specify the library (and its path via -Lpath/to/library, if the linker does not find the library on its own):
gcc .\Test.o .\some_module.o -o main -L./bin/ -lcmocka
You should definitely not use your second approach and compile the library by yourself.

How compile on linux KNN CUDA?

Recently, I found knn CUDA which is a group of Mex file that implement knn search based on brute force, but in the README.md I have not found the way to compile this files in matlab using a linux distribution. I would appreciate ideas about how cope with this issue.
I'm the author of this kNN code :)
Back in 2008, the code was written using the Windows XP OS.
Since I provide the source code, you should be able to produce linux mex files.
In the ReadMe, I give the following command line for Windows :
nvmex -f nvmexopts.bat knn_cuda_with_indexes.cu -I'C:\CUDA\include' -L'C:\CUDA\lib' -lcufft -lcudart -lcuda -D_CRT_SECURE_NO_DEPRECATE
Adapt it for your Linux distribution to generate your mex file.
A lot of things may have changed in 5 years so you may have to modify a few things.
However, the feedbacks I got from users indicate that it works just fine.
Try also to read about how to compile a CUDA code under Linux.
I guess NVidia provides a pretty nice tutorial.
You can also compile cuda+mex without nvmex. In MATLAB command, simply run the following two lines
>> !nvcc -c yourfile.cu -Xcompiler -fPIC -I$matlabroot/extern/include -I$matlabroot/toolbox/distcomp/gpu/extern/include
>> mex yourfile.o -L/usr/local/cuda/lib64 -L$matlabroot/bin/glnxa64 -lcudart -lcufft -lmwgpu
replace $matlabroot with appropriate path. (Note that ! invoke system command in matlab)
The first line create object file and then mex links library.
You might have to modify your CUDA path to /usr/loca/cuda-6.0/ or /usr/local/cuda-YOUR_VERSION/. Also for the cuda library /usr/local/cuda/lib64 or /usr/local/cuda/lib Please check.
If you want to optimize your code simply put -O3 -DNDEBUG
>> !nvcc -O3 -DNDEBUG -c yourfile.cu -Xcompiler -fPIC -I$matlabroot/extern/include -I$matlabroot/toolbox/distcomp/gpu/extern/include
the library link command is same.
Also please note that additional include path -I$path and library path -L$path or library -l$library might be required to suit your need.

How do I set gcc to use the file extension (.c or .cpp) to determine the correct compiler/linker?

I have a simple, representative C program, stored in a file called hello.c:
#include <stdio.h>
int main(void)
{
printf('Hello, world\n');
return 0;
}
On my Linux machine, I attempted to compile the program with gcc:
gcc hello.c
which returns an error:
undefined reference to "___gxx_personality_v0" ... etc
As has been discussed before in the context of C++, this problem arises in the linking stage, when gcc attempts to link C libraries to a C++ program, thus giving the error. In one of the answers, someone mentioned that the extension does matter, and that gcc requires the .c extension when compiling C files, and some other extension (e.g. .cpp) when compiling C++ files.
Question: How do I set gcc to use the file extension to determine which compiler to use, since gcc seems to be defaulting to C++ on my system? Specifying the language through the file extension alone doesn't seem to be enough. If I specify the language using the -x flag, gcc functions as expected.
gcc -x c hello.c
Typically, you let make decide this.
GNU Make has built in implicit rules, which automatically pick the right compiler.
Try a Makefile with these contents:
all: some_file.o some_other_file.o
And then place a some_file.cpp and some_other_file.c in the same directory, and gnu make will automatically pick the correct compiler. The linker, you may still have to provide yourself. When mixing C and C++, it's usually easiest to link with g++, like so:
program.exe: some_file.o some_other_file.o
g++ -o $# #^
This is the same as:
program.exe: some_file.o some_other_file.o
g++ -o program.exe some_file.o some_other_file.o

Is it possible to link to the math library from inside the C source code in gcc?

When I tried to include <math.h> I found that I need to link math library by using command gcc -lm
But I am searching for another way to link the math library 'in code', that does not require the user to compile using any options..
Can gcc -lm be done in C code using #pragma or something?
EDIT: I have changed -ml to -lm
The usual way to simplify complication for the user (or indeed for the developer) is to write a makefile.
First, it's gcc -lm and no there is no #pragma meant to give linking directives
No, you need to tell the linker to link the library in order to link the library.
The linker doesn't know about the code, only the compiled object files. It won't see a language specific pragma.
You don't say which UNIX shell you are using, but if this is just for conveniance, simply write a shell function:
gcm() {
gcc -lm $*
}
Put that in your shell's startup file and you can compile and link with the maths library with:
gcm mycode.c
Using -lm is the only option. Additionally, using #pragma for that is microsoft-specific and rather dirty. Imagine there is a new super-efficient math library which requires -lsupermath instead of -lm - then you'd have to modify your code instead of modifying a makefile or a make config file.
No, gcc has no pragmas for linking to libraries. You have to link to the math library with command line options (it's -lm not -ml )

Resources