Tag Info

Hot answers tagged

216

Summary: The primary reason for switching from GCC to Clang is the incompatibility of GCC's GPL v3 license with the goals of the FreeBSD project. There are also political issues to do with corporate investment, as well as user base requirements. Finally, there are expected technical advantages to do with standards compliance and ease of debugging. Real world ...


60

You can't just ./fork.c (it's not a program; it's the source for a program): this assumes that the file is a script (which it isn't) and treats it accordingly. (However, as noted in another answer, there are compilers (like Tiny C Compiler) that can execute C code without explicitly compiling it) Since it's a C program, you have to compile the program. Try ...


27

One thing worth considering is that FreeBSD is currently using GCC 4.2.1 as noted in ire_and_curses answer thus the performance comparisons aren't of 4.5 or even 4.6 aren't truly relevant to the project. Therefore, the questions you should be asking are: What are the performance gains of the new Clang vs the older GCC that the project uses? How do the same ...


22

That's not a program, that's the source code for a program. C is a compiled language, meaning it must be "compiled" into machine-readable instructions before you can run it. As you are using C, the "C Compiler" (cc) can do this. cc -o fork for.c # compile the code chmod +x fork # ensure it it executable ./fork # run the compiled program ...


17

The new process will be created within the fork() call, and will start by returning from it just like the parent. The return value (which you stored in retval) from fork() will be: 0 in the child process The PID of the child in the parent process -1 in the parent if there was a failure (there is no child, naturally) Your testing code works correctly; it ...


14

You're right that you'll end up with the same executable at the end (albeit with a different name); in the first case gcc will actually create a bunch of temporary object files that it removes after linking, versus the second case where you're making the object files yourself. The main reason to do things the second way is to allow for incremental building. ...


14

GNU/Linux systems usually use either glibc (Fedora/Redhat family, Arch) or its close cousin, eglibc (Debian/Ubuntu family); since eglibc is now being merged back into glibc (see EGLIBC 2.19 Branch Created under "News"), in the near future they will all be glibc again. The easiest way to check the exact version is to ask ldd, which ships with the C library. ...


13

The standard way to log from a C program is syslog. Start by including the header file: #include <syslog.h> Then early in your program, you should configure syslog by calling openlog: openlog("programname", 0, LOG_USER); The first argument is the identification or the tag, which is automatically added at the start of each message. Put your ...


12

Of course it won't crash the kernel, you're writing to the virtual memory space of your own program, not the real kernel memory space. Learn more about virtual memory here P.S: Why printf doesn't print anything? By default standard output is line-buffered, and your hello world doesn't contain a line separator. So if the program crashed, you will not ...


12

If you know the definition of the data type you want you can use getconf to find these values out on most Unix systems. $ getconf CHAR_BIT 8 The list of variables is defined in the man page man limits.h as well as here, man sysconf, in addition to being on disk. You can use locate limits.h to find it, it's often here: /usr/include/linux/limits.h.


11

Why does a Linux distribution have gcc installed in advance? A Linux distribution is rather vague. Some install it, most offer to install it (possibly even if you select the defaults during installation). However not all distributions will install it and you usually have a choice. Is it because most of the applications in Linux are written in C? ...


10

I thought that fork() creates a same process, so I initially that that in that program, the fork() call would be recursively called forever. I guess that new process created from fork() starts after the fork() call? Yes. Let's number the lines: int main (int argc, char **argv) { int retval; /* 1 */ ...


10

There are several possibilities. One method is to compile using :!gcc file.c But a nicer strategy would be to have a Makefile and compile just using :make Where the easiest Makefile would look like. program: gcc file.c Others can explain this a lot better.


10

Because getc() buffers the read data before returning it, so a call to getc() does not necessarily result in a call to read(). read() is a system call, which takes much longer time to accomplish than a normal function call because the kernel has more operations to do. When you enter the kernel space, it changes your stack, saves all the context, deals with ...


9

A device that has the functions of a physical terminal without actually being one. Created by terminal emulators such as xterm. More detail is in the manpage pty(7). Traditionally, UNIX has a concept of a controlling terminal for a group of processes, and many I/O functions are built with terminals in mind. Pseudoterminals handle, for example, some control ...


9

t_open() and its associated /dev/tcp and such are part of the TLI/XTI interface, which lost the battle for TCP/IP APIs to BSD sockets. On Linux, there is a /dev/tcp of sorts. It isn't a real file or kernel device. It's something specially provided by Bash, and it exists only for redirections. This means that even if one were to create an in-kernel /dev/tcp ...


8

Man pages for the standard C library are included in the man-pages package. For the C++ STL library the man-pages and HTML documentation are included in the libstdc++-docs packages. Thus, yum install man-pages libstdc++-docs should install them. You can test if they are available via: man std::iostream man fopen Kind of off-topic: IMHO the libstdc++ ...


8

Even though GCC is GPLv3, the resulting binaries produced by GCC never had any license constraint. In clear you can use GCC to build software that falls under the license you want. Even the C library that comes with GCC and that is included in the binary is license-free. http://www.gnu.org/licenses/gcc-exception-faq.html Section 2 of the GNU GPLv3: You ...


7

In the latest versions of gcc compiler require that libraries follow the object or source files. So to compile this it should be: gcc pthread_sample.c -lpthread Normally though pthread code is compiled this way: gcc -pthread pthread_sample.c


7

You could move the espeak binary to something like espeak-real, and replace it with a small script that sets LD_PRELOAD before exec'ing espeak-real. #! /bin/bash export LD_PRELOAD=/your/lib.so exec espeak-real "$@" (stdin/out/err redirections take care of themselves.)


7

The canonical way to do this in Vim is to use the compiler configuration setting. Your vim installation almost certainly comes with a compiler plugin for gcc. Type :help compiler in Vim to find out more about how this works. To associate gcc with c source files, you need something like this in your .vimrc: au BufEnter *.c compiler gcc


7

The start address is the address of main(), right? Not really: The start of a program isn't really main(). By default, GCC will produce executables whose start address corresponds to the _start symbol. You can see that by doing a objdump --disassemble Q1. Here's the output on a simple program of mine that only does return 0; in main(): 0000000000400e30 ...


7

Yes. You could scan /usr/include/<arch>/limits.h For example, on my NetBSD amd64, /usr/include/amd64/limits.h would show: #define CHAR_BIT 8 /* number of bits in a char */ #define SCHAR_MAX 0x7f /* max value for a signed char */ #define SCHAR_MIN (-0x7f-1) /* min value for a signed char */ #define ...


7

A system isn't actually limited to one C library. Most, though, primarily use only one, which will also be the one the default compiler uses. And since you're downloading source code to compile, that's the one you're concerned with. Start with a trivial program: #include <stdio.h> int main() { printf("Hello, world\n"); return 0; } compile ...


6

Use either skill -u UID or pkill -u UID. Skill was a linux-specific and is now outdated, and pkill is more portable (Linux, Solaris, BSD) I think, both utilites have no access to any list other than full list of processes (or readdir of /proc). I think, they itterate over /proc and check every process for match. PS: Another tool is killall . To get list ...


6

In the C API, system calls return a negative value to indicate an error, and the error code in errno gives more information on the nature of the error. Your man page should explain the possible errors on your system. There are two standard error codes: EAGAIN indicates that the new process cannot be created due to a lack of available resources, either ...


6

As seen earlier, vfork does not allow the child process to access the parent's memory. exit is a C library function (that's why it's often written as exit(3)). It performs various cleanup tasks such as flushing and closing C streams (the files open through functions declared in stdio.h) and executing user-specified functions registered with atexit. All these ...


6

I'm a pretty hard-core Emacs user but still, for developing C++ I prefer Qt-Creator (don't be afraid because of the name, it works well for non-QT-projects) as Emacs lacks good project support and stable code completion The pros: Can import CMakeFiles.txt into an automatically created project Best code completion you'll find on Linux, sometimes even ...


6

You can use the Glibc manual as a reference. It's not for absolute beginners, but if you're reasonably fluent in C you should be able to read a section and write a working program. You can find the source of the GNU tools on the GNU website; the easiest way to get it is to obtain the source packages on your Linux distribution (e.g. apt-get source coreutils ...


6

Neither. If you want to have it behave properly like a real daemon you should place it using the init system - /etc/init.d ( and make appropriate runlevel links in the appropriate /etc/rc.X folders ) Run a search or have a look at something like this: http://serverfault.com/questions/204695/comprehensive-guide-to-init-d-scripts



Only top voted, non community-wiki answers of a minimum length are eligible