Hot answers tagged

76

Well, if you want to go the extra mile, do it in LaTeX and provide a professional level PDF file. You haven't mentioned your distribution so I'll give instructions for Debian based systems. The same basic idea can be done on any Linux though. Install a LaTeX system and necessary packages sudo apt-get install texlive-latex-extra latex-xcolor texlive-latex-...


37

There have been many discussions about this. Mainly, the reason is a philosophical one. C was invented as a simple language for system development (not so much application development). There are many arguments for using C++, but there are about as many for not using C++ and sticking to C. In the end, it's a historical issue. Most application stuff is ...


29

I'd usually use enscript: something like $ enscript --highlight=cpp --header='|Real Name|Class 101' --footer='|Page $% of $=|' -poutput.ps *.cpp will be a start - this writes postscript output to output.ps, so you can preview and overwrite that while you're tinkering with the config and then print it once you're happy. See ...


21

According to the standards support pages for clang and gcc, you can use gcc >= 5.0 or clang >= 3.4. Most C++14 support was added in 4.9 for gcc, but a few features did not make it in until 5.0.


17

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. ...


16

The Cross compiler article on wikipedia is quite good. Generally a cross compiler is a compiler producing code for a different kind of system than yours. Usually this means a different target hardware architecture, but it can also mean a different target operating system (or both). Theoretically you could combine these and use a cross-compiler on ...


16

Because the nc command inside <(...) will also read from stdin. Simpler example: $ nc -l 9999 >/tmp/foo & [1] 5659 $ echo text | cat <(nc -N localhost 9999) - [1]+ Done nc -l 9999 > /tmp/foo Where did the text go? Through the netcat. $ cat /tmp/foo text Your program and nc compete for the same stdin, and nc gets some ...


13

You may want to read from cin to get a poor man’s pause – it will wait for you to type an Enter, rather than resuming while you’re getting coffee (as sleep() will).


13

I needed GCC 5+ installed on debian jessie and, as is available for debian testing (at least on jun-16) you can use apt-pinning to install packages available there (see https://wiki.debian.org/AptPreferences). To install GCC 5+ from testing on debian jessie using apt-pinning: Add debian testing repo to your apt sources by creating a file in the directory /...


11

I think that alternatives are the wrong approach here. More generally, I think changing the system default is really the wrong approach. Rather than changing things at that level, I suggest export CC=/usr/bin/clang export CXX=/usr/bin/clang++ Unless something goes out of its way to break it, this will work with almost all make systems. If you want this to ...


9

From this answer to "Install gcc 4.7 on CentOS [6.x]", the easiest way to get g++ 4.7, and the required tools and libraries, for CentOS 5.x is via the devtools package: cd /etc/yum.repos.d wget http://people.centos.org/tru/devtools/devtools.repo yum --enablerepo=testing-devtools-6 install devtoolset-1.0 Since you're running g++ manually (as opposed to ...


9

You can use the :TOhtml command in vim. This renders what you see (i.e. syntax highlighting) as html. From there, a web browser that can print to pdf works, as you can usually customize the header/footer content. This is probably similar to the :hardcopy command mentioned by Useless, but I can't verify on my system right now. Another possibility is to ...


8

gcc is both a C and C++ compiler. It will look at the file's extension and process accordingly. g++ is only a wrapper that calls gcc g++ is installed with the gcc package in Arch gcc file.C # uppercase .C (or .cpp) will process as C++ gcc file.c # lowercase .c will process as C


8

Here is a breakdown of the command. First the original command, for reference g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something Now, for the breakdown. g++ This is the actual command command, g++. It is the program that is being executed. Here is what it is, from the man page: gcc - GNU project C and C++ compiler This is a compiler ...


8

Your Linux Mint comes pre-installed with a GCC package. So first I would recommend you to check if the package is already present in your system by typing the following command in terminal. apt-cache search gcc In case you're not having any such package then use the following command in terminal firstly you've add the following repository: sudo add-apt-...


7

Use -mtune. -march is used to determine the allowed instruction set, whereas -mtune is to be used to tune performance of the code (as always, see man gcc). Depending on the precise CPU type, you might also consider values other than core2. And if you use a recent GCC version (at least 4.4, I think), you might best use native instead.


7

For developing C/C++ you need the gcc compiler, which is included in most Linux distributions or can be easily installed. There is not a default IDE: most people use their favorite editor (vim, emacs, Geany etc...) and there are IDEs like Eclipse or KDevelop available. C# can be done with Mono, but it is not fully compatible with .NET: check the ...


7

Here the simplest analogue of lsusb: #include <stdio.h> #include <usb.h> main(){ struct usb_bus *bus; struct usb_device *dev; usb_init(); usb_find_busses(); usb_find_devices(); for (bus = usb_busses; bus; bus = bus->next) for (dev = bus->devices; dev; dev = dev->next){ printf("Trying device %s/...


7

You're leaving a zombie, trivially, because you didn't wait on your child process. Your shell is immediately exiting because you've set up its STDIN in a nonsensical way. pipe returns a one-way communications channel. You write to pipefd[1] and read it back from pipefd[0]. You did a buch of dup2 calls which lead the shell to attempt to read (STDIN) from the ...


7

You need to define a breakpoint, for example break main Then run and gdb will start your program and stop when it enters main.


6

For external representations, UTF-8 is definitely the standard. Some 8-bit encodings are still strong (mostly in Europe) and some 16-bit encodings are still strong (mostly in East Asia), but they are clearly legacy encodings, on their slow way out. UTF-8 is standard not only on unix, but also on the web. For internal representations, there's no such ...


6

After reading the following email from Linus Torvalds the creator of linux. I can't help thinking the answer above is incorrect. He seems to think that C++ programmers are not good systems programmers. And that the extra features in C++ often cause more problems in the long term compared to the benefits they bring in the short term. While one may disagree ...


6

If you trust the local machine not to be spoofing, both ifconfig and ip addr will give you the MAC address of the hardware. If you don't trust the local machine, neither ethtool, ifconfig, nor ip is going to provide you the information you need. Because there are very legitimate reasons for MAC spoofing (for example, hot-fail on ethercards) all the drivers ...


6

The main entry point is God. Be it a C or C++ source file, it is the center of the application. Only in the same way that nitrogen is the center of a pine tree. It is where everything starts, but there's nothing about C or C++ that makes you put the "center" of your application in main(). A great many C and C++ programs are built on an event loop or an I/O ...


6

I sometimes resort to this #define BRK() do { printf("%s %d\n", __FILE__, __LINE__); getchar(); } while (0) ... BRK(); // Stop and wait for enter Also, function instrumentation might help: GCC allows to hook into every function enter/exit, see https://stackoverflow.com/questions/2281739/automatically-adding-enter-exit-function-logs-to-a-project


6

To combine the 2 previous answers into one that works: echo "deb http://ftp.us.debian.org/debian unstable main contrib non-free" >> /etc/apt/sources.list.d/unstable.list apt-get update apt-get install -t unstable gcc-5 BUT!!! Use this at your own risk as you are installing packages from unstable. You might also want to remove /etc/apt/sources.list.d/...


6

Okay, I found what file it's looking for using strace, and the answer was libstdc++.a , so I fixed it by installing the libstdc++-static package


5

This has more to do with C and C++ than Unix, and as such belongs to SO. To answer your question, the <> indicates headers in the standard library and "" the libraries written specifically for the project. From the K&R: Any source line of the form #include "filename" or #include <filename> is replaced by the contents of ...


5

This is just a partial answer, since your question is fairly broad. C++ defines an "execution character set" (in fact, two of them, a narrow and a wide one). When your source file contains something like: char s[] = "Hello"; Then the numeric byte value of the letters in the string literal are simply looked up according to the execution encoding. (The ...


5

It's probably killed by kernel's oom killer. dmesg should contain information about it. Sorry, but you may need to redesign your algorithm.


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