Error: cast from 'void * to' int will lose precision [-fpermissive] in makefile

when compiling the following makefile on ubuntu 12.04 LTS (64-bit) encountering this error:

Ana.cxx:21:46: error: cast from โ€˜void*โ€™ to โ€˜intโ€™ loses precision [-fpermissive]

      

on this line:

21 : TThread::Printf("Start of Ana %x \n" ,(int)ptr);

      

here is the makefile:

ObjSuf        = o
SrcSuf        = cxx
DllSuf        = so
ExeSuf        =
OutPutOpt     = -o 
CXXFLAGS      = -g -Wall -fPIC -DOSF1 
CXX           = g++
CCFLAGS       = -g -Wall -DOSF1 
CC            = gcc
LD            = g++
LDFLAGS       = -g
SOFLAGS       = -shared
ROOTCFLAGS   := $(shell root-config --cflags) -DFILL_ON_FLY 
ROOTLIBS     := $(shell root-config --libs) -lNew -lThread -lMinuit -lPhysics
ROOTGLIBS    := $(shell root-config --glibs)  -lNew -lThread
EVENTO        = BINAEvent.$(ObjSuf) Ana.$(ObjSuf) BINAEventDict.$(ObjSuf)     mwpc_software.o tcpip.o Genbood.o  
EVENTS        = BINAEvent.$(SrcSuf) Ana.$(SrcSuf) BINAEventDict.$(SrcSuf) mwpc_software.c tcpip.c Genbood.c 
EVENTLIB      = $(ROOTGLIBS)
EVENTEXE      = ana
OBJS          = $(EVENTO)
    .SUFFIXES: .$(SrcSuf) .$(ObjSuf) .$(DllSuf)
    .PHONY:    Aclock Hello Tetris
     $(EVENTEXE):     $(OBJS)
         $(LD) $^ $(ROOTLIBS) $(OutPutOpt) $@
         @echo "$@ done"
     clean:
         @rm -f $(OBJS) core *~ *Dic* ana *.o
     .SUFFIXES: .$(SrcSuf)
     BINAEvent.$(ObjSuf): BINAEvent.h
       BINAEventDict.$(SrcSuf): BINAEvent.h BINAEventLinkDef.h
       @echo "Generating dictionary $@..."
       @rootcint -f $@ -c $^
      .$(SrcSuf).$(ObjSuf):
       $(CXX) $(CXXFLAGS) $(ROOTCFLAGS) -c $<
    .c.$(ObjSuf):
     $(CXX) $(CXXFLAGS) $(ROOTCFLAGS) -c $<

      

What is the correct way to do this?

Thanks in advance.

-3


source to share


4 answers


The error has nothing to do with the makefile. The error is in the source, on the line indicated by the error.

On your platform, the pointer doesn't fit into int

. If you specified an integer, you should probably use intptr_t

or uintptr_t

from stdint.h

, which are guaranteed to be large enough.



I don't know what it is TThread::Printf

, this is not a standard. But the usual way to print the address of a pointer from printf

is to pass the pointer just like void*

using a format specifier %p

. This probably also applies to TThread::Printf

.

+4


source


The type int

(32 bits) is not large enough to store a 64-bit pointer void*

.



+1


source


The problem has nothing to do with yours Makefile

; this is a bug in the C ++ source code. Fortunately, you showed us the error line.

The correct way to print the value of a pointer in C is to use a specifier %p

that requires a type argument void*

:

TThread::Printf("Start of Ana %p\n", (void*)ptr);

      

ptr

appears to already have a type void*

, so no cast is required, but it is needed for other pointer types.

This assumes what TThread::Printf

works like printf

; given the name it should definitely, but it doesn't guarantee.

For C ++, I usually advise using C ++ I / O with an operator <<

(and for pointers you have to be careful with char*

which overload has, treating it as a pointer to a string), but which may not be available in this case.

You can convert the value of a pointer to an integer type if you really want to, but this is rarely a good idea and certainly not necessary if you just want to print its value. The types uintptr_t

and intptr_t

defined in <stdint.h>

or <cstdint>

are guaranteed to be able to hold the converted pointer value void*

without losing information (and will not be determined unless an integer type is assigned), int

no, and converting a pointer to int

can easily lose information.

0


source


My solution is using "long long" instead of "int"

-1


source







All Articles