g++

Value category of conditional operator

老子叫甜甜 提交于 2019-12-23 12:58:28
问题 Consider the following code: int x; int& f() { return x ? x : throw 0; } With gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) I get the following compilation error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’ Note that this compiles just fine in clang. Here is (what I believe to be) the relevant statement from the standard: N4659 [8.16.2.1] (Conditional Operator): The second or the third operand (but not both) is a (possibly parenthesized) throw

C++ How can I prevent my team developers from using integer version of abs by mistake?

倖福魔咒の 提交于 2019-12-23 12:34:38
问题 My team is writting code to be compiled for both Windows (using VS2015 ) and Android (using GCC 4.9 invoked by QtCreator ). We figured out that Android binaries had a problem with abs function. double a = 1.0; double b = 0.5; std::cout << abs( a - b ) << std::endl; std::cout << std::abs( a - b ) << std::endl; Displays: 1 0.5 This is a known issue, found this topic (among others): Strange bug in usage of abs() I encountered recently There are lots of places where we use abs , I'll replace them

Can gcc/g++ tell me when it ignores my register?

為{幸葍}努か 提交于 2019-12-23 12:21:11
问题 When compiling C/C++ codes using gcc/g++, if it ignores my register, can it tell me? For example, in this code int main() { register int j; int k; for(k = 0; k < 1000; k++) for(j = 0; j < 32000; j++) ; return 0; } j will be used as register, but in this code int main() { register int j; int k; for(k = 0; k < 1000; k++) for(j = 0; j < 32000; j++) ; int * a = &j; return 0; } j will be a normal variable. Can it tell me whether a variable I used register is really stored in a CPU register? 回答1:

Compiling previously preprocessed file changes output

旧巷老猫 提交于 2019-12-23 10:28:17
问题 I have a source file which I preprocess using the options -E and -P (using GCC 4.1.2 for a vxWorks-based embedded platform). All other options are the same as when I compile the file. These options are: -Wall -march=pentium -nostdinc -O0 -fno-builtin -fno-defer-pop -g -c -o as well as all include-paths. Now when I compile this preprocessed file, the resulting object-file is much smaller (about 30%) than when I compile the original directly. And when I then link the program, the linker

C++ get the mangled names of a function/method

时光毁灭记忆、已成空白 提交于 2019-12-23 10:24:43
问题 Hi I need to determine the mangled name of a function from within an c++ app itself. Is there any equivalent to the __FUNCDNAME__ macro in g++ ? 回答1: To get the demangled name use __PRETTY_FUNCTION__. Better is to use: #include <boost/current_function.hpp> BOOST_CURRENT_FUNCTION This gives the demangled name. I looked at cxxabi.h but there does not seem to be a mangle function. Do you really want the mangled name? 来源: https://stackoverflow.com/questions/3262585/c-get-the-mangled-names-of-a

How do you compile just a .h file in a makefile?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 10:16:57
问题 I have a makefile that creates object files for two classes (and main) and one of those classes is just defined in a .h file. In my makefile I have a line that says FileName.o: FileName.h g++ -c FileName.h but when I try to compile it says it can't find FileName.o Do I have to create FileName.cpp in order to get this to compile? 回答1: You are using your class from FileName.h somewhere, aren't you? So at least one of your .cpp files should contain #include "FileName.h" , and .h's code will be

Why do I get this error? void* is not a pointer to object type.

北城以北 提交于 2019-12-23 10:06:17
问题 void *stackAddr[NUM_THREADS]; stackAddr[i] = malloc(STACKSIZE); The compiler (g++ 4.4.3) complains where the malloc is called... warning: pointer of type ‘void *’ used in arithmetic error: ‘void*’ is not a pointer-to-object type If you are interested in seeing the whole code, here it goes... #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #define NUM_THREADS 4 void *stackAddr[NUM_THREADS]; pthread_t thread[NUM_THREADS]; pthread_attr_t attr; void *BusyWork(void *t

What's with these g++ “multiple definition” errors?

此生再无相见时 提交于 2019-12-23 09:56:30
问题 I'm in the early stages (read:just started yesterday) of a project, and I'm setting up my initial makefile. It's very simplistic. Here's the full contents of the file: all: main.o resource.o g++ -o output.exe main.o resource.o main.o: main.cpp main.h resource.h g++ -mwindows -o main.o main.cpp resource.o: resource.rc windres resource.rc resource.o clean: rm *.o At this point I think it's important to mention that I'm working on Windows, so I'm doing all of this in either Powershell with MinGW

Undefined reference to MySQL libraries using g++

牧云@^-^@ 提交于 2019-12-23 09:55:44
问题 I am getting undefined reference to 'mysql_suchandsuch@#' messages when trying to link my program with the MySQL libraries supplied with the 5.5 server. When MySQL was installed, I used the default path, which for me on Windows is C:\Program Files\MySQL\MySQL Server 5.5\ . Originally, I had thought that the spaces are causing my grief, but I think I've correctly worked out how to point to the library path without spaces (still with no luck). If there's another probable cause, please let me

Why doesn't gcc support naked functions?

北慕城南 提交于 2019-12-23 09:39:26
问题 I use naked functions to patch parts of a program while it's running. I can easily do this in VC++ in Windows. I'm trying to do this in Linux and it seems gcc doesn't support naked functions. Compiling code with naked functions gives me this: warning: ‘naked’ attribute directive ignored. Compiled under CentOS 5.5 i386. 回答1: The naked attribute is only supported by GCC on certain platforms (ARM, AVR, MCORE, RX and SPU) according to the docs: naked : Use this attribute on the ARM, AVR, MCORE,