Using a different version of glibc? - c

I want to do some modifications to the glibc library. The first step is to be able to use a specific version when I compile a program. I'm under ubuntu 12.10 and my directories are :
/mydirectory/glibc-2.17 (where I have extracted the last version from the website)
/mydirectory/glibc-2.17-build (where I have executed the configure and make command)
/mydirectory/test/helloworld.c (where I have my helloworld program)
The helloworld.c is the following:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char glibc[256] = "xxxx"; /* How to detect the glibc version here ? */
printf("hello, world\n");
printf("glibc version = %s\n", glibc);
return 0;
}
First how can I print the version of glibc ? (I think that there is a macro/constant in glibc for that).
Second, what command line should I use to compile my helloworld.c file to use the glibc that is in /mydirectory/glibc-2.17-build ?

Use -L pathname to explicitly specify a pathname to ld as Barmar has said in the comment.
It's suggested to use static linking -static or there might be problems during execution I think.
Actually my own solution to this problem would be: compile and link the source code as normal, and invoke with LD_PRELOAD set to your specified version of shared objects.
See http://linux.die.net/man/8/ld.so

Related

How to install libxml2 for C in OS X?

how to install libxml2 in OS X?
EDITED:
main.c
#include <stdlib.h>
#include <libxml2>
int main() {
printf("Hello, world!");
return 0;
}
The output I get is:
error: 'libxml2' file not found
If you have Homebrew installed, you can install libxml2 using:
brew install libxml2
To use libxml2, or any shared library, you need to...
Include the right header files in your code.
Add the path to the header files.
Add the path to the shared libraries.
Add the shared library.
The libxml2 docs aren't the best, but there are some code examples to draw from. And from that we see we need to #include <libxml/component.h> where component is whatever piece of the library you're including. For example, if you want to write XML documents, it's #include <libxml/xmlwriter.h>.
#include <stdlib.h>
#include <libxml/xmlwriter.h>
int main() {
// Just something to demonstrate we can call functions and the linking worked
xmlTextWriterPtr writer = xmlNewTextWriterFilename("example.com", 0);
// Just something to do with the variable.
printf("%p\n", writer);
}
Then you need to find the header files. OS X comes with libxml2 installed, but it's in /usr/include/libxml2. So that needs to be added to the include path with a -I/usr/include/libxml2.
The headers contain the definitions of the various functions, but the real code lies in shared libraries. Those are in the normal location, but you have to tell the compiler it to use it with -lxml2. Fortunately they're in the default location, so we don't have to add to the normal search path for shared libraries (that would be -L/some/path/).
Put it all together...
cc -I/usr/include/libxml2 -lxml2 -Wall test.c

How to use PlaySound in C

I am using code::blocks IDE which runs on GNU GCC compiler. In my project I want to play a .wav sound file in C. I tried to play a .wav sound file with a function called PlaySound. When I compiled the code code::blocks gave me an error - PlaySoundA not declared. My code is-
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
int main(int argc, char *argv[])
{
PlaySound("C:\Snakes and Ladders\snake.wav",NULL,SND_SYNC | SND_LOOP | SND_FILENAME);
return 0;
}
I checked my path twice. I read about this function on the internet and as per me I am using it in the correct way.
In Google, I read that the function exists in a file called winmm.lib. So I put a line of code after all the headers. It was-
#pragma comment (lib , "winmm.lib")
I also added the name winmm.lib to the additional dependencies of code::blocks. So now when I compile the code it gives me another error - winmm.lib not found. Can somebody please tell me how to use PlaySound correctly.
Remove the pragma comment
Double the backslashes. The backslash is an escape character
Compile with the winmm library. Using MinGW, the command would look like this:
gcc foo.c -o foo.exe -lwinmm
Go to Settings - compiler... - linker settings. on the right side in other linker option write this:-lwinmm

Tcl interpreter undefined reference error while compiling with gcc

I am new to Tcl scripting and would like to use C to embed Tcl codes.
This is the code that I have copied from a website to test the Tcl-C working.
test.c
#include <stdio.h>
#include <tcl.h>
void main ()
{
Tcl_Interp *myinterp;
char *action = "set a [expr 5 * 8]; puts $a";
int status;
printf ("Your Program will run ... \n");
myinterp = Tcl_CreateInterp();
status = Tcl_Eval(myinterp,action);
printf ("Your Program has completed\n");
getch();
}
I am using MinGW to compile this file.
I have copied the contents of the C:\Tcl\include folder into the C:\MinGW\include folder as well.
My gcc command for compiling :
gcc -o test.exe test.c
The error message shown :
C:\Users\user\AppData\Local\Temp\ccEHJKCb.o:tcl_connection_test.c:(.text+0x23): undefined reference to `_imp__Tcl_CreateInterp'
C:\Users\user\AppData\Local\Temp\ccEHJKCb.o:tcl_connection_test.c:(.text+0x3d): undefined reference to `_imp__Tcl_Eval'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccEHJKCb.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
I don't seem to have any libtcl file in the Tcl folder.
The Tcl version is ActiveTcl 8.5.15.0.297577.
Any help would be really appreciated.
Your example how to embed Tcl is outdated, and you are missing certain things in your link line (-ltcl85 for example). If you simply add -ltcl85 to your link line it should start to work.
It does not work in your case, because you installed the x64 (64-Bit version) of ActiveTcl, which provides x64 dlls, not 32-Bit ones. But the standard mingw gcc only works with 32-Bit libraries.
So to get this to work:
Download the 32-Bit ActiveTcl distribution
Compile your code with gcc -o test.exe test.c -Lc:/tcl/lib -Ic:/tcl/include -ltcl86
Adjust your path so the c:\tcl\bin\tcl86.dll is found in PATH, make also sure Tcl finds its libdir (set TCL_LIBRARY=c:\tcl\lib\tcl8.6)
run your program
But for more complex examples, you still need to initialise the library and a do some boilerplate code, so please call Tcl_FindExecutable(argv[0]); before the call to Tcl_CreateInterp() otherwise a few commands (e.g. clock might just not work as expected).
Have a look at http://www.tcl.tk/cgi-bin/tct/tip/66.html for some more details. Also have a look at the Tcl source distribution and the source for the tclsh shell.
You're very close to getting it right.
The Tcler's Wiki has a few examples, some of which are very confusing to be frank, but this one from this page is the best I've spotted recently. (The comments are mine.)
#include <stdlib.h>
#include <tcl.h>
int main (int argc, char *argv[]) {
Tcl_Interp *interp;
const char *script = "proc p1 a { puts $a }";
// Initialize the Tcl library; ***STRONGLY RECOMMENDED***
Tcl_FindExecutable(argv[0]);
// Create the interpreter, the execution context
interp = Tcl_CreateInterp();
// Initialise the interpreter
if (TCL_OK != Tcl_Init(interp)) {
fprintf(stderr, "Tcl_Init error: %s\n", Tcl_GetStringResult(interp));
exit(EXIT_FAILURE);
}
// Define a procedure
Tcl_Eval(interp, script);
fprintf(stderr, "res 1: %s\n", Tcl_GetStringResult(interp));
// Check if the procedure exists
Tcl_Eval(interp, "puts [info commands p*]");
fprintf(stderr, "res 2: %s\n", Tcl_GetStringResult(interp));
// Call the procedure
Tcl_Eval(interp, "p1 abc");
fprintf(stderr, "res 3: %s\n", Tcl_GetStringResult(interp));
// We could use Tcl_DeleteInterpreter to clean up here, but why bother?
return EXIT_SUCCESS;
}
What else were you missing? Simple. You forgot to tell the C compiler to use the Tcl library when building the executable; the compiler (or, more strictly, the linker) is in places a stupid piece of code. The exact option to use to get the linker to add the library in will depend on your system configuration, but is probably going to be -ltcl, -ltcl8.5 or -ltcl8.6; which it is depends on the filename and all sorts of things that we can't check exactly without being on your system. The names do fit a simple pattern though.
It's also possible that you might need to pass the -L option in to tell the linker about additional library locations. (There's an equivalent -I for telling the compiler where to find include files, so you don't have to copy everything into one gigantic unmanageable directory.)
The order of arguments can matter. Libraries should be listed after the source file:
gcc -o test.exe test.c -L/mingw/path/to/library/directory -ltcl86
(If you're using old, unsupported versions of Tcl — why would you do that?! — then the code above won't work because Tcl_Eval then took a writable string. But that was fixed many years ago and upgrading to a current version is the fix.)

run c program - stdio.h where do i get it?

Looking into learning C. As I understand it when I say #include <stdio.h> it grabs stdio.h from the default location...usually a directory inside your working directory called include. How do I actually get the file stdio.h? Do I need to download a bunch of .h files and move them from project to project inside the include directory? I did the following in a test.c file. I then ran make test and it outputted a binary. When I ran ./test I did not see hello print onto my screen. I thought I wasn't seeing output maybe because it doesn't find the stdio.h library. But then again if I remove the greater than or less than signs in stdio the compiler gives me an error. Any ideas?
I'm on a Mac running this from the command line. I am using: GNU Make 3.81. This program built for i386-apple-darwin10.0
#include <stdio.h>
main()
{
printf("hello");
}
Edit: I have updated my code to include a datatype for the main function and to return 0. I still get the same result...compiles without error and when I run the file ./test it doesn't print anything on screen.
#include <stdio.h>
int main()
{
printf("hello");
return 0;
}
Update:
If I add a \n inside of the printf it works! so this will work:
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
Your code should have preferably
printf("hello\n");
or
puts("hello");
If you want to know where does the standard header file <stdio.h> comes from, you could run your compiler with appropriate flags. If it is gcc, try compiling with
gcc -H -v -Wall hello.c -o hello
Pedantically, a standard header file is even not required to exist as a file; the standard permits an implementation which would process the #include <stdio.h> without accessing the file system (but e.g. by retrieving internal resources inside the compiler, or from a database...). Few compilers behave that way, most really access something in the file system.
If you didn't have the file, you'd get a compilation error.
My guess is the text was printed, but the console closed before you got the chance to see it.
Also, main returns an int, and you should return 0; to signal successful completion.
#include <header.h>, with angle brackets, searches in standard system locations, known to the compiler-- not in your project's subdirectories. In Unix systems (including your Mac, I believe), stdio.h is typically in /usr/include. If you use #include "header.h", you're searching subdirectories first and then the same places as with <header.h>.
But you don't need to find or copy the header to run your program. It is read at compilation time, so your ./test doesn't need it at all. Your program looks like it should have worked. Is it possible that you just typed "test", not "./test", and got the system command "test"? (Suggestion: Don't name your programs "test".)
Just going to leave this here : STILL! in 2018, December... Linux Mint 18.3
has no support for C development.
innocent / # cc ThoseSorts.c
ThoseSorts.c:1:19: fatal error: stdio.h: No such file or directory
compilation terminated.
innocent / # gcc ThoseSorts.c
ThoseSorts.c:1:19: fatal error: stdio.h: No such file or directory
compilation terminated.
innocent / # apt show libc6
(Abbreviated)::
Package: libc6
Version: 2.23-0ubuntu10
Priority: required
Section: libs
Source: glibc
Origin: Ubuntu
Installed-Size: 11.2 MB
Depends: libgcc1
Homepage: http://www.gnu.org/software/libc/libc.html
Description: GNU C Library: Shared libraries
Contains the standard libraries that are used by nearly all programs on
the system. This package includes shared versions of the standard C library
and the standard math library, as well as many others.
innocent / # apt-get install libc6-dev libc-dev
So, magic... and a minute later they are all installed on the
computer and then things work as they should.
Not all distros bundle up all the C support libs in each ISO.
Hunh.
hardlyinnocent / # gcc ThoseSorts.c
hardlyinnocent / # ./a.out
20
18
17
16
... ... ...

Compile and run a file.c using Tiny C Compiler on Windows

Could I get a step by step on how to compile my file.c using Tiny C Compiler and Windows prompt?
Some questions I already have:
Where do I stick all TCC files from the download?
Do I have to compile stdio.h to use the printf function? (I'd like to do a 'Hello World').
This is what my file.c looks like:
// #include <stdio.h> // for printf
int main(void){
printf("Hello Eric. You've compiled and run the program! \n");
}
Thanks,
EDIT 1
So far I'm running it and getting the error: include file 'stdio.h' not found.
you put the files wherever you like.
no, you do not need to compile stdio.h in order to use the printf() function.
the tcc-distribution (tcc-0.9.25-win32-bin\tcc) consists of this:
tcc.exe
tiny_impdef.exe
tiny_libmaker.exe
include\
stdio.h ...
lib\
libtcc1.a ...
doc\
examples\
if you do not tear that order apart, tcc should work out of the box (i compiled a hello.c seconds ago). if you separated the files or something else does not work:
% tcc.exe -Ipath/to/include/folder/of/tcc input.c -L/path/to/lib/folder/of/
by looking at the source code of tcc i found this:
/* on win32, we suppose the lib and includes are at the location
of 'tcc.exe' */
char path[1024], *p;
GetModuleFileNameA(NULL, path, sizeof path);
p = tcc_basename(normalize_slashes(strlwr(path)));
so, per default it assumes the libs and the headers to be in the place right next to the tcc.exe.

Resources