how to compile a program with a given file.a [duplicate] - c

This question already has answers here:
CMake link to external library
(6 answers)
Closed 1 year ago.
I did a program that need some functions from a file that the staff gives us.
the file is libmap.a.
I developed some modules that use functions from the libmap file.
how can I compile it?
Some friends said me that I need to change something to the Cmake file.
is someone know what I need to change?

You do not compile, only link with the library file.
for gcc you need to use -l option in your case -lmap and if needed set the path using -L option.
in Cmake you need to set your library search path:
LINK_DIRECTORIES(your_target "directory")
and the add the library
TARGET_LINK_LIBRARIES(your_target map)

Related

How to access all the files before compilation? [duplicate]

This question already has an answer here:
How do I save preprocessor output using the Dev-C++ IDE?
(1 answer)
Closed 2 years ago.
There are several steps involved from the stage of writing a C program to the stage of getting it executed.
when I compile the code I only get an .exe file. But I want to get all the files which are being made before the compilation (preprocess ones), an intermediate code file
where all those macros with are replaced with their actual values and preprocessor are replaced with their actual header files.
in general can we get all those files (preprocess one, compile one and linker one) separately?
To access all the intermediate files use the command (Ubuntu):
gcc –Wall –save-temps filename.c –o filename. This command generates all the intermediate files in current working directory.

What are .o files and to open them on windows? [duplicate]

This question already has answers here:
What's an object file in C?
(5 answers)
Closed 3 years ago.
I'm a beginner in programming, I wonder what is inside the .o files and want to see it, but can't open the files in windows because they give some output with unrecognized symbols. Please suggest something !
They are object files, produced by the compiler, which the linker will combine into an executable.
They are not intended to be human readable.

Using C Libraries like OpenGL and LibCurl [duplicate]

This question already has an answer here:
How to Include external C library on windows
(1 answer)
Closed 4 years ago.
I've seen plenty of online tutorials explaining how to use GLFW and libcurl, but where do I actually place the files I downloaded?
For example I downloaded this file for GLFW -
And these are the contents The C+ file wasn't in there I put that there :p
So how would I add the library to any of my .c files?
I've looked everywhere, I might just not be using the right keywords.
And second, how can I have multiple libraries at the same time?
And lastly, what do I put in the <> in the include?
I'm using windows 10 and am using GCC as my compiler. I really should get the Intel one shoudn't I?
This is what the SRC looks like.
In C, a library comes in two parts:
One (or more) header files
The actual library implementation, under Linux either a shared object (.so) or a static library (.a), under Windows I guess it would be a DLL file
You need to include the header file in each C source file of yours where you want to use functionality from the library:
In your source, add a line
#include <library-header.h>
for each header you want to include.
for all header files the library is delivered with.
You will have to tell your compiler where to find the header files. For gcc, you would have to enter something like that on the command line.
gcc -I /path/to/library/directory/include -c my_source.c -o my_source.o
Then, you need to link your program with the actual library. This again, depends on your tool chain, e.g. the compiler you use. For gcc again, the command line would look something like
gcc -lname_of_the_libray -L/path/to/library/directory/ my_source.o -o my_exe
However, libraries are often distributed in source code, which means you would have to compile the library beforehand.

fatal error: sndfile.h.in: No such file or directory [duplicate]

This question already has answers here:
What mean file with extension "h.in"?
(3 answers)
Closed 8 years ago.
I was compiling with gcc on Linux
Because sndfile.h was not there but sndfile.h.in was found, I just tried with sndfile.h.in - which is in the same directory as the *.C file.
But I got the error even though it is in the same directory. Its been a while since I programmed in Linux that these little things are bothering me - appreciate if u could help me started. Thanks
I think you are using the angular brackets for the including the file.If you place < >. It will search in /usr/include. You have to use the double quotes for including the file in the current directory. And be sure that file is available.
Like this.
#include "sndfile.h.in"

How does my C program know where to find my binary library? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to use dylib in Mac OS X (C++)
I have the following files:
lost.h
lost.dylib
I am struggling with the concept of how my C program knows where my lost.dylib file lives? At the top of my file I do an include #include "lost.h" but how does my program know about my lost.dylib?
At link time it uses a combination of standard directories and user specified directories
Linker options has more information. Basically using -L option you can specify more directories to search for dylib that you are interested in.
If you are using Xcode, this link has more details
As far as runtime finding of the dynamic library is concerned. OS X does it a little bit different that other platforms. OS X embeds an "install name" inside each dynamic library. You can use otool -L to find the details
When linking, you should specify with the linker options (-L for directories, -l for libs). Your library should be named liblost.dylib, and the linker option should be -llost. (strip the 'lib' preffix and the .dylib extension).
Also, when the binary is compiled, you can use the otool command if you want to find out where your executable is searching for the dylib.
And if you want to change that directory, you can use install_name_tool.

Resources