All Questions
17,186
questions
2149
votes
20
answers
715k
views
How do I profile C++ code running on Linux?
How do I find areas of my code that run slowly in a C++ application running on Linux?
860
votes
19
answers
1.8m
views
Sleep for milliseconds
I know the POSIX sleep(x) function makes the program sleep for x seconds. Is there a function to make the program sleep for x milliseconds in C++?
646
votes
16
answers
1.6m
views
usr/bin/ld: cannot find -l<nameOfTheLibrary>
I'm trying to compile my program and it returns this error :
usr/bin/ld: cannot find -l<nameOfTheLibrary>
in my makefile I use the command g++ and link to my library which is a symbolic link ...
487
votes
11
answers
138k
views
What is Linux’s native GUI API?
Both Windows (Win32 API) and OS X (Cocoa) have their own APIs to handle windows, events and other OS stuff. I have never really got a clear answer as to what Linux’s equivalent is?
I have heard some ...
415
votes
26
answers
740k
views
Easily measure elapsed time
I am trying to use time() to measure various points of my program.
What I don't understand is why the values in the before and after are the same? I understand this is not the best way to profile my ...
396
votes
16
answers
403k
views
How do I output coloured text to a Linux terminal?
How do I print coloured characters to a Linux terminal that supports it?
How do I tell whether the terminal supports colour codes?
380
votes
5
answers
241k
views
Difference between shared objects (.so), static libraries (.a), and DLL's (.so)?
I have been involved in some debate with respect to libraries in Linux, and would like to confirm some things.
It is to my understanding (please correct me if I am wrong and I will edit my post later),...
237
votes
15
answers
141k
views
Finding current executable's path without /proc/self/exe
It seems to me that Linux has it easy with /proc/self/exe. But I'd like to know if there is a convenient way to find the current application's directory in C/C++ with cross-platform interfaces. I've ...
224
votes
28
answers
130k
views
Is there a C++ gdb GUI for Linux? [closed]
Briefly: Does anyone know of a GUI for gdb that brings it on par or close to the feature set you get in the more recent version of Visual C++?
In detail: As someone who has spent a lot of time ...
220
votes
7
answers
258k
views
C++ compiling on Windows and Linux: ifdef switch [duplicate]
I want to run some c++ code on Linux and Windows. There are some pieces of code that I want to include only for one operating system and not the other. Is there a standard #ifdef that once can use?
...
213
votes
11
answers
213k
views
How do I install g++ for Fedora?
How do I install g++ for Fedora Linux? I have been searching the dnf command to install g++ but didn't find anything.
How do I install it?
I have already installed gcc
209
votes
45
answers
1.0m
views
C++ IDE for Linux? [closed]
I want to expand my programming horizons to Linux. A good, dependable basic toolset is important, and what is more basic than an IDE?
I could find these SO topics:
Lightweight IDE for linux and
What ...
202
votes
15
answers
296k
views
How to print a stack trace whenever a certain function is called
Is there any way to dump the call stack in a running process in C or C++ every time a certain function is called? What I have in mind is something like this:
void foo()
{
print_stack_trace();
...
201
votes
13
answers
242k
views
Linux c++ error: undefined reference to 'dlopen'
I work in Linux with C++ (Eclipse), and want to use a library.
Eclipse shows me an error:
undefined reference to 'dlopen'
Do you know a solution?
Here is my code:
#include <stdlib.h>
#...
196
votes
5
answers
148k
views
What are the GCC default include directories?
When I compile a very simple source file with gcc I don't have to specify the path to standard include files such as stdio or stdlib.
How does GCC know how to find these files?
Does it have the /usr/...
190
votes
4
answers
288k
views
C++ Dynamic Shared Library on Linux
This is a follow-up to Dynamic Shared Library compilation with g++.
I'm trying to create a shared class library in C++ on Linux. I'm able to get the library to compile, and I can call some of the (...
190
votes
6
answers
119k
views
Clang vs GCC for my Linux Development project
I'm in college, and for a project we're using C. We've explored GCC and Clang, and Clang appears to be much more user friendly than GCC. As a result, I'm wondering what the advantages or ...
181
votes
10
answers
207k
views
OS specific instructions in CMAKE: How to?
I am a beginner to CMAKE. Below is a simple cmake file which works well in mingw environment windows. The problem is clearly with target_link_libraries() function of CMAKE where I am linking ...
171
votes
4
answers
209k
views
C++ error: undefined reference to 'clock_gettime' and 'clock_settime'
I am pretty new to Ubuntu, but I can't seem to get this to work. It works fine on my school computers and I don't know what I am not doing. I have checked usr/include and time.h is there just fine. ...
169
votes
2
answers
114k
views
Significance of -pthread flag when compiling
In various multi threaded C and C++ projects I've seen the -pthread flag applied to both the compiling and linking stage while others don't use it at all and just pass -lpthread to the linking stage.
...
163
votes
9
answers
108k
views
How do I find the location of the executable in C? [duplicate]
Is there a way in C/C++ to find the location (full path) of the current executed program?
(The problem with argv[0] is that it does not give the full path.)
162
votes
7
answers
19k
views
Is it safe to parse a /proc/ file?
I want to parse /proc/net/tcp/, but is it safe?
How should I open and read files from /proc/ and not be afraid, that some other process (or the OS itself) will be changing it in the same time?
161
votes
11
answers
245k
views
Compile error: "g++: error trying to exec 'cc1plus': execvp: No such file or directory"
When I compile C/C++ program with popen in php... I got this error:
g++: error trying to exec 'cc1plus': execvp: No such file or directory
but if I run php code in shell.. it works fine..
in Arch ...
145
votes
7
answers
116k
views
Set breakpoint in C or C++ code programmatically for gdb on Linux
How can I set a breakpoint in C or C++ code programatically that will work for gdb on Linux?
I.e.:
int main(int argc, char** argv)
{
/* set breakpoint here! */
int a = 3;
a++; /* In ...
140
votes
18
answers
256k
views
How can I create a directory tree in C++ on Linux?
I want an easy way to create multiple directories in C++ on Linux.
For example, I want to save a file lola.file in the directory:
/tmp/a/b/c
but if the directories are not there I want them to be ...
138
votes
11
answers
285k
views
How to disassemble a binary executable in Linux to get the assembly code?
I was told to use a disassembler. Does gcc have anything built in? What is the easiest way to do this?
131
votes
11
answers
82k
views
Spinlock versus Semaphore
What are the basic differences between a semaphore & spin-lock?
When would we use a semaphore over a spin-lock?
126
votes
9
answers
174k
views
How to install the Raspberry Pi cross compiler on my Linux host machine?
I am attempting to get cross-compiling for Raspberry Pi working on my Ubuntu machine.
During my initial attempts I was using the arm-linux-gnueabi compiler, which is available in the Ubuntu repo. I ...
116
votes
5
answers
98k
views
Linking libstdc++ statically: any gotchas?
I need to deploy a C++ application built on Ubuntu 12.10 with GCC 4.7's libstdc++ to systems running Ubuntu 10.04, which comes with a considerably older version of libstdc++.
Currently, I'm compiling ...
113
votes
7
answers
83k
views
C/C++ with GCC: Statically add resource files to executable/library
Does anybody have an idea how to statically compile any resource file right into the executable or the shared library file using GCC?
For example I'd like to add image files that never change (and if ...
112
votes
4
answers
128k
views
Call a C function from C++ code
I have a C function that I would like to call from C++. I couldn't use "extern "C" void foo()" kind of approach because the C function failed to be compiled using g++. But it compiles fine using gcc. ...
111
votes
10
answers
116k
views
C++: what regex library should I use? [closed]
I'm working on a commercial (not open source) C++ project that runs on a linux-based system. I need to do some regex within the C++ code. (I know: I now have 2 problems.)
QUESTION: What ...
110
votes
5
answers
128k
views
How to view symbols in object files?
How can I view symbols in a .o file? nm does not work for me. I use g++/linux.
109
votes
9
answers
200k
views
Compilation fails with "relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object"
I'm trying to compile this source code from the makefile in a VPS, but its not working. The VPS is a 64 Cent OS
Here's the full error
# make
gcc -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c
g++ -c ...
109
votes
5
answers
164k
views
Which is the best Linux C/C++ debugger (or front-end to gdb) to help teaching programming? [closed]
I teach a sort of "lite" C++ programming course to novices ("lite" meaning no pointers, no classes, just plain old C, plus references and STL string and vectors). Students have no previous experience ...
108
votes
3
answers
70k
views
How to set breakpoints on future shared libraries with a command flag
I'm trying to automate a gdb session using the --command flag. I'm trying to set a breakpoint on a function in a shared library (the Unix equivalent of a DLL) . My cmds.gdb looks like this:
set args /...
105
votes
8
answers
16k
views
Make a program run slowly
Is there any way to run a C++ program slower by changing any OS parameters in Linux? In this way I would like to simulate what will happen if that particular program happens to run on a real slower ...
104
votes
6
answers
131k
views
Proper way to create unique_ptr that holds an allocated array
What is the proper way to create an unique_ptr that holds an array that is allocated on the free store? Visual studio 2013 supports this by default, but when I use gcc version 4.8.1 on Ubuntu I get ...
104
votes
13
answers
142k
views
C++ Boost: undefined reference to boost::system::generic_category()
I am trying to include Boost libraries in my project and have been facing issues in the same. I am on Ubuntu 12.10 with Codeblocks IDE and tried installing the libraries manually reading instructions ...
103
votes
14
answers
143k
views
How to set up googleTest as a shared library on Linux
Debian does not provide any precompiled packages for gTest anymore. They suggest you integrate the framework into your project's makefile. But I want to keep my makefile clean. How do I set up gTest ...
103
votes
12
answers
206k
views
How can I get the IP address of a (Linux) machine?
This Question is almost the same as the previously asked How can I get the IP Address of a local computer? -Question. However I need to find the IP address(es) of a Linux Machine.
So: How do I - ...
102
votes
10
answers
99k
views
How to programmatically cause a core dump in C/C++
I would like to force a core dump at a specific location in my C++ application.
I know I can do it by doing something like:
int * crash = NULL;
*crash = 1;
But I would like to know if there is a ...
100
votes
11
answers
195k
views
How to get memory usage at runtime using C++?
I need to get the mem usage VIRT and RES at run time of my program and display them.
What i tried so far:
getrusage (http://linux.die.net/man/2/getrusage)
int who = RUSAGE_SELF;
struct rusage ...
100
votes
1
answer
16k
views
What exactly does GCC's -Wpsabi option do? What are the implications of supressing it?
Background
In the last year I was using the nlohmann json library[1] and was cross-compiling on x86_64 using GCC 5.x arm-linux-gnueabi-* with no warnings. When I updated GCC to a newer version, GCC ...
96
votes
4
answers
108k
views
Get home directory in Linux
I need a way to get user home directory in C++ program running on Linux. If the same code works on Unix, it would be nice. I don't want to use HOME environment value.
AFAIK, root home directory is /...
96
votes
4
answers
45k
views
Why is CUDA pinned memory so fast?
I observe substantial speedups in data transfer when I use pinned memory for CUDA data transfers. On linux, the underlying system call for achieving this is mlock. From the man page of mlock, it ...
95
votes
4
answers
106k
views
Compiling multithread code with g++
I have the easiest code ever:
#include <iostream>
#include <thread>
void worker()
{
std::cout << "another thread";
}
int main()
{
std::thread t(worker);
std::cout <&...
94
votes
4
answers
198k
views
How do you find what version of libstdc++ library is installed on your linux machine?
I found the following command: strings /usr/lib/libstdc++.so.6 | grep GLIBC from here. It seems to work but this is an ad-hoc/heuristic method.
Is there a specific command that can be used to query ...
94
votes
11
answers
50k
views
How to Add Linux Executable Files to .gitignore?
How do you add Linux executable files to .gitignore without giving them an explicit extension and without placing them in a specific or /bin directory? Most are named the same as the C file from which ...
93
votes
8
answers
250k
views
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: version CXXABI_1.3.8' not found
It turns out that "make install" - the make target that installs and implies the target "install-target-libstdc++v3" doesn't actually mean you're ready to go.
I've been stuck for a ...