How to keep g++ from taking header file from /usr/include? - g++

I am building using zlib.h which I have a local copy to v1.2.5, but in /usr/include/zlib.h there is v1.2.1.2.
If I omit adding -I/my/path/to/zlib to my make I get error from using old version which doesn't have Z_FIXED:
g++ -g -Werror -Wredundant-decls -D_FILE_OFFSET_BITS=64 -c -o ARCH.linux_26_i86/debug/sysParam.o sysParam.cpp
sysParam.cpp: In member function `std::string CSysParamAccess::getCompressionStrategyName() const':
sysParam.cpp:1816: error: `Z_FIXED' was not declared in this scope
sysParam.cpp: In member function `bool CSysParamAccess::setCompressionStrategy(const std::string&, paramSource)':
sysParam.cpp:1849: error: `Z_FIXED' was not declared in this scope
Alternatively, if I add the include path to the zlib z1.2.5 I am using, I get double defines, it seems as if the zlib.h is included twice with two different sets of -D values, but I don't see how that is happening:
g++ -g -Werror -Wredundant-decls -I../../src/zlib-1.2.5 -D_FILE_OFFSET_BITS=64 -c -o ARCH.linux_26_i86/debug/sysParam.o sysParam.cpp
In file included from sysParam.cpp:24:
../../src/zlib-1.2.5/zlib.h:1582: warning: redundant redeclaration of `void* gzopen64(const char*, const char*)' in same scope
../../src/zlib-1.2.5/zlib.h:1566: warning: previous declaration of `void* gzopen64(const char*, const char*)'
../../src/zlib-1.2.5/zlib.h:1583: warning: redundant redeclaration of `long long int gzseek64(void*, long long int, int)' in same scope
../../src/zlib-1.2.5/zlib.h:1567: warning: previous declaration of `off64_t gzseek64(void*, off64_t, int)'
../../src/zlib-1.2.5/zlib.h:1584: warning: redundant redeclaration of `long long int gztell64(void*)' in same scope
../../src/zlib-1.2.5/zlib.h:1568: warning: previous declaration of `off64_t gztell64(void*)'
../../src/zlib-1.2.5/zlib.h:1585: warning: redundant redeclaration of `long long int gzoffset64(void*)' in same scope
../../src/zlib-1.2.5/zlib.h:1569: warning: previous declaration of `off64_t gzoffset64(void*)'
../../src/zlib-1.2.5/zlib.h:1586: warning: redundant redeclaration of `uLong adler32_combine64(uLong, uLong, long long int)' in same scope
../../src/zlib-1.2.5/zlib.h:1570: warning: previous declaration of `uLong adler32_combine64(uLong, uLong, off64_t)'
../../src/zlib-1.2.5/zlib.h:1587: warning: redundant redeclaration of `uLong crc32_combine64(uLong, uLong, long long int)' in same scope
../../src/zlib-1.2.5/zlib.h:1571: warning: previous declaration of `uLong crc32_combine64(uLong, uLong, off64_t)'
Here some of the relavent lines from zlib.h referred to above:
// This would be line 1558 of zlib.h
/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
* change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
* both are true, the application gets the *64 functions, and the regular
* functions are changed to 64 bits) -- in case these are set on systems
* without large file support, _LFS64_LARGEFILE must also be true
*/
#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
#endif
#if !defined(ZLIB_INTERNAL) && _FILE_OFFSET_BITS-0 == 64 && _LFS64_LARGEFILE-0
# define gzopen gzopen64
# define gzseek gzseek64
# define gztell gztell64
# define gzoffset gzoffset64
# define adler32_combine adler32_combine64
# define crc32_combine crc32_combine64
# ifdef _LARGEFILE64_SOURCE
ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
# endif
#else
ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
#endif
// This would be line 1597 of zlib.h
I'm not sure how to track this down further. I tried moving the include of zlib.h to the top and bottom of the includes list of the cpp file, but it made no difference.
An excerpt of passing -E to g++ shows in part:
extern int inflateInit2_ (z_streamp strm, int windowBits, const char *version, int stream_size);
extern int inflateBackInit_ (z_streamp strm, int windowBits, unsigned char *window, const char *version, int stream_size);
# 1566 "../../src/zlib-1.2.5/zlib.h"
extern gzFile gzopen64 (const char *, const char *);
extern off64_t gzseek64 (gzFile, off64_t, int);
extern off64_t gztell64 (gzFile);
extern off64_t gzoffset64 (gzFile);
extern uLong adler32_combine64 (uLong, uLong, off64_t);
extern uLong crc32_combine64 (uLong, uLong, off64_t);
# 1582 "../../src/zlib-1.2.5/zlib.h"
extern gzFile gzopen64 (const char *, const char *);
extern long long gzseek64 (gzFile, long long, int);
extern long long gztell64 (gzFile);
extern long long gzoffset64 (gzFile);
extern uLong adler32_combine64 (uLong, uLong, long long);
extern uLong crc32_combine64 (uLong, uLong, long long);
# 1600 "../../src/zlib-1.2.5/zlib.h"
struct internal_state {int dummy;};
Not sure why lines 1566 and 1582 are coming out together in the CPP output, but hence the warning about duplicate declarations.

-nostdinc answers the question in your Q's title -- quoting this manpage, it means:
Do not search the standard system
directories for header files. Only the
directories you have specified with -I
options (and the directory of the
current file, if appropriate) are
searched.
However, I don't think it will solve your actual problem, which seems to be due to two mutually incompatible parts of the same non-system header file getting included -- that feels more likely to be due to some needed -D being missing, but I can't say exactly what since I'm not familiar with that specific header file.

My guess is that in some instances you have:
#include <zlib.h>
and in others you have
#include "zlib.h"
You will find the old (system) zlib.h in the first case and the new (user) zlib.h in the second case.
The fix for this is to use -isystem instead of -I for your new zlib includes, i.e. -isystem /my/path/to/zlib/includes instead of -I /my/path/to/zlib/includes.

I got a similar error when compiling leptonica on Solaris 5.10 (64-bit SPARC), and I agree with Alex: some necessary define is missing or something. On a whim, I added _FILE_OFFSET_BITS=64 (./configure CPPFLAGS='-D_FILE_OFFSET_BITS=64'), which "worked" (it compiled). This is a bit of cargo-cult programming of course, 'cause I don't know why I should have had to do that. I haven't actually tried to use leptonica yet, so it might core or something 'cause I added that define when I built it.

Related

unresolved symbol pthread_create, first referenced in ./armrtk/src/task.obj

I have been trying to figure this out for a few days now and cannot figure it out. I am using CCS as the IDE and I am working on windows. I am trying to create an RTOS Kernel on a MSP432 and need to use pthreads. I have been able to use pthreads in other examples but I am trying to do my own program and I get this issue when building :
unresolved symbol pthread_create, first referenced in ./armrtk/src/task.obj
I have included the file path into CCS and I cannot use a .cfg file because I am not using XDCTools. I just need help with this and I greatly appreciate it.
I also get a warning:
in pthread_create in TASK.C: #169-D argument of type "void *" is incompatible with parameter of type "void *(*)(void *)"
TASK.H
#ifndef TASK_H
#define TASK_H
#include <pthread.h>
struct task_t {
pthread_t* thread;
int threadCheck;
int state;
};
void *task1(void);
void *task2(void);
struct task_t *create_task(void* functionptr);
void delete_task(void *task);
#endif
TASK.C
#include <task.h>
#include <stdlib.h>
#include <pthread.h>
#define BLOCKED -1
#define READY 0
#define RUNNING 1
int testValue1 = 0;
int testValue2 = 0;
struct task_t *new_task;
pthread_t pntr;
struct task_t *create_task(void* functionptr) {
new_task = malloc(sizeof(struct task_t));
if(!new_task)
return NULL;
//set State of the new thread to ready
new_task->state = 0;
// check to see if pthread is created
**new_task->threadCheck = pthread_create(new_task->thread, NULL, functionptr, NULL);**
if(new_task->threadCheck!= 0){
//thread failed
return NULL;
}
return new_task;
}
void delete_task(void *task) {
if(task != NULL){
free(task);
pthread_exit(NULL);
}
}
The unresolved symbol error is a linker error, not a compiler error. You have failed to link the pthreads library.
With respect to the warning functionptr is a void* where pthread_create() expects a pointer-to-function with signature void fn(void*).
Your task functions have a different signature in any case: void fn(void), so in any event you will need to cast the function pointer in the call to pthread_create() (although you are loosing a useful means of passing information into a task function by omiting the void* argument).
Modify task.h:
typedef void* (*task_t)(void);
struct task_t *create_task( task_t functionptr);
The in task.cpp
new_task->threadCheck = pthread_create( new_task->thread,
NULL,
(void (*)(void *))functionptr,
NULL ) ;
The cast in the pthread_create() call alone would supress the warning, but it bad form to pass a function pointer as a generic void* since it would prevent the compiler warning you if you were to pass anything other then a function pointer of the expected form to to the create_task()`

tensorflow: Errors when building op using g++ 6.3.0 (via MinGW) on Windows 7

I'm brand-new to tensorflow and am aiming to run a pre-built model created by another user. As an initial step in this process, I'm trying to build several ops. However, I have been unable to build even basic ops, let alone those that actually perform useful tasks. To give an example, I followed these directions and created the following as zero_out.cc:
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
REGISTER_OP("ZeroOut")
.Input("to_zero: int32")
.Output("zeroed: int32");
using namespace tensorflow;
class ZeroOutOp : public OpKernel {
public:
explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}
void Compute(OpKernelContext* context) override {
// Grab the input tensor
const Tensor& input_tensor = context->input(0);
auto input = input_tensor.flat<int32>();
// Create an output tensor
Tensor* output_tensor = NULL;
OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
&output_tensor));
auto output = output_tensor->template flat<int32>();
// Set all but the first element of the output tensor to 0.
const int N = input.size();
for (int i = 1; i < N; i++) {
output(i) = 0;
}
// Preserve the first input value if possible.
if (N > 0) output(0) = input(0);
}
};
Afterward, I attempted to run the following in cmd (calling g++ 6.3.0, which I installed via MinGW):
g++ -std=c++11 -msse2 -shared zero_out.cc -o zero_out.so -I "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include" -l tensorflow_framework -L "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow" -fPIC -Wl,-rpath "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow"
The above line did not produce an .so file and instead yielded an assortment of errors (which I've truncated to satisfy the Stack Overflow character limit):
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/message_lite.h:48:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/generated_enum_util.h:36,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/map.h:48,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/generated_message_table_driven.h:34,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def.pb.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def_builder.h:24,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:23,
from zero_out.cc:1:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/once.h:43:24: error: 'once_flag' in namespace 'std' does not name a type
using once_flag = std::once_flag;
^~~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/once.h: In function 'void google::protobuf::internal::call_once(Args&& ...)':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/once.h:46:3: error: 'call_once' is not a member of 'std'
std::call_once(std::forward<Args>(args)...);
^~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/once.h:46:3: note: suggested alternative:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/once.h:45:6: note: 'google::protobuf::internal::call_once'
void call_once(Args&&... args ) {
^~~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h:62:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/generated_message_reflection.h:47,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def.pb.h:30,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def_builder.h:24,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:23,
from zero_out.cc:1:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/mutex.h: At global scope:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/mutex.h:107:8: error: 'mutex' in namespace 'std' does not name a type
std::mutex mu_;
^~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/mutex.h: In member function 'void google::protobuf::internal::WrappedMutex::Lock()':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/mutex.h:99:43: error: 'mu_' was not declared in this scope
void Lock() GOOGLE_PROTOBUF_ACQUIRE() { mu_.lock(); }
^~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/mutex.h: In member function 'void google::protobuf::internal::WrappedMutex::Unlock()':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/stubs/mutex.h:100:45: error: 'mu_' was not declared in this scope
void Unlock() GOOGLE_PROTOBUF_RELEASE() { mu_.unlock(); }
^~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/generated_message_reflection.h:47:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def.pb.h:30,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def_builder.h:24,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:23,
from zero_out.cc:1:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h: At global scope:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h:221:13: error: 'once_flag' in namespace 'google::protobuf::internal' does not name a type
internal::once_flag* once_;
^~~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h: In member function 'void google::protobuf::internal::LazyDescriptor::Init()':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h:189:5: error: 'once_' was not declared in this scope
once_ = nullptr;
^~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h: At global scope:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h:787:13: error: 'once_flag' in namespace 'google::protobuf::internal' does not name a type
internal::once_flag* type_once_;
^~~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h:1461:13: error: 'once_flag' in namespace 'google::protobuf::internal' does not name a type
internal::once_flag* dependencies_once_;
^~~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h: In member function 'google::protobuf::FieldDescriptor::Type google::protobuf::FieldDescriptor::type() const':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/descriptor.h:2053:7: error: 'type_once_' was not declared in this scope
if (type_once_) {
^~~~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def.pb.h:30:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_def_builder.h:24,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:23,
from zero_out.cc:1:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/generated_message_reflection.h: At global scope:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/google/protobuf/generated_message_reflection.h:271:3: error: 'once_flag' does not name a type
once_flag* once;
^~~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:32:0,
from zero_out.cc:1:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/mutex.h:231:8: error: 'cv_status' in namespace 'std' does not name a type
std::cv_status wait_for(mutex_lock& lock,
^~~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/mutex.h: In function 'tensorflow::ConditionResult tensorflow::WaitForMilliseconds(tensorflow::mutex_lock*, tensorflow::condition_variable*, tensorflow::int64)':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/mutex.h:249:3: error: 'cv_status' is not a member of 'std'
std::cv_status s = cv->wait_for(*mu, std::chrono::milliseconds(ms));
^~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/mutex.h:250:11: error: 's' was not declared in this scope
return (s == std::cv_status::timeout) ? kCond_Timeout : kCond_MaybeNotified;
^
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/mutex.h:250:21: error: 'std::cv_status' has not been declared
return (s == std::cv_status::timeout) ? kCond_Timeout : kCond_MaybeNotified;
^~~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/mutex.h:310:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:32,
from zero_out.cc:1:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/default/mutex.h: At global scope:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/default/mutex.h:25:6: error: 'cv_status' in namespace 'std' does not name a type
std::cv_status wait_until_system_clock(
^~~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/default/mutex.h:31:6: error: 'cv_status' in namespace 'std' does not name a type
std::cv_status condition_variable::wait_for(
^~~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/numeric_types.h:20,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/allocator.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:24,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:44:17: error: conflicting declaration 'typedef long int int32_t'
typedef __int32 int32_t;
^~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:62:15: note: previous declaration as 'typedef int int32_t'
typedef int int32_t;
^~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/numeric_types.h:20,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/allocator.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:24,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:45:26: error: conflicting declaration 'typedef long unsigned int uint32_t'
typedef unsigned __int32 uint32_t;
^~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:63:19: note: previous declaration as 'typedef unsigned int uint32_t'
typedef unsigned uint32_t;
^~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/notification.h:27:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/lib/core/notification.h:21,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/cancellation.h:22,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:25,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/default/notification.h: In member function 'bool tensorflow::Notification::WaitForNotificationWithTimeout(tensorflow::int64)':
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/default/notification.h:69:20: error: 'class tensorflow::condition_variable' has no member named 'wait_for'; did you mean 'wait'?
cv_.wait_for(l, std::chrono::microseconds(timeout_in_us)) !=
^~~~~~~~
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/platform/default/notification.h:70:25: error: 'std::cv_status' has not been declared
std::cv_status::timeout);
^~~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor.h:22,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/device_base.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:27,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor: At global scope:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:44:17: error: conflicting declaration 'typedef long int int32_t'
typedef __int32 int32_t;
^~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:62:15: note: previous declaration as 'typedef int int32_t'
typedef int int32_t;
^~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor.h:22,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/device_base.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:27,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:45:26: error: conflicting declaration 'typedef long unsigned int uint32_t'
typedef unsigned __int32 uint32_t;
^~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:63:19: note: previous declaration as 'typedef unsigned int uint32_t'
typedef unsigned uint32_t;
^~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor_shape.h:21,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor.h:24,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/device_base.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:27,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:44:17: error: conflicting declaration 'typedef long int int32_t'
typedef __int32 int32_t;
^~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:62:15: note: previous declaration as 'typedef int int32_t'
typedef int int32_t;
^~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor_shape.h:21,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor.h:24,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/device_base.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:27,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:45:26: error: conflicting declaration 'typedef long unsigned int uint32_t'
typedef unsigned __int32 uint32_t;
^~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:63:19: note: previous declaration as 'typedef unsigned int uint32_t'
typedef unsigned uint32_t;
^~~~~~~~
In file included from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:0,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor_types.h:19,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/tensor.h:25,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/device_base.h:26,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op_kernel.h:27,
from zero_out.cc:2:
C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/unsupported/Eigen/CXX11/Tensor:44:17: error: conflicting declaration 'typedef long int int32_t'
typedef __int32 int32_t;
^~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:420,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\functional:55,
from C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include/tensorflow/core/framework/op.h:19,
from zero_out.cc:1:
c:\mingw\include\stdint.h:62:15: note: previous declaration as 'typedef int int32_t'
typedef int int32_t;
^~~~~~~
Considering how basic zero_out.cc seems (at least to my novice eyes), my suspicion is that the main problem lies elsewhere. Notably, my attempts to build existing ops also triggered conflicting declaration errors, so even if I've made some mistake in writing zero_out.cc, that's not the full extent of the problem. From what I've read, later C++ standards supposedly fixed issues related to variable templates, but I've gotten the same exact errors regardless of whether I use -std=c++11, -std=c++14, or -std=c++17 as options in the call to g++ (among other options that I've attempted). It also doesn't seem like one should have to manually rewrite a bunch of standard installed files just to build a basic op... I hope.
There's probably some laughably simple mistake that is escaping my grasp, so I apologize for what is likely a very basic question from a novice. Nonetheless, any insights that you can offer would be greatly appreciated.
(Just in case it's relevant, I initially tried building tensorflow from source and encountered numerous problems with bazel and bazelisk. More recently, after having uninstalled my previous tensorflow version, I installed tensorflow from pip, which worked, as least as far as I can tell. However, I'm not sure if any artifacts left behind by my earlier installation attempts might be conflicting with the new installation, and to be frank, I'm unfamiliar enough with tensorflow that I'm not even sure how to begin diagnosing such an issue.)
UPDATE: I wound up biting the bullet and just using Visual Studio for kernel compilation--even though I wanted to avoid needlessly relying on a proprietary tool, there didn't seem to be much of a choice. In case it helps others, the below command prompt worked for zero_out.cc:
cpp -std=c++11 -msse2 -shared "C:/Users/Admin/AppData/Local/Programs/Python/Python38/Lib/site-packages/tensorflow/tensorflow/tensorflow/core/kernels/zero_out.cc" -o "C:/Users/Admin/AppData/Local/Programs/Python/Python38/Lib/site-packages/tensorflow/tensorflow/tensorflow/core/kernels/zero_out.so" -I "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow\include" -l tensorflow_framework -L "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\tensorflow"
With that said, I'd still be interested in any potential solutions involving g++ or a comparable tool, so I'll leave this question as-is so that others can post any answers that they have. (Additionally, while the above line of code worked for zero_out.cc, it resulted in errors for some other kernels. That seems to be a distinct issue, though, so I'll post it as a separate question if I can't resolve it on my own.)

C++ Builder Function error [bcc32 - Ambiguity error] inside dll file

I am creating a currency converter Win32 program in Embarcadero C++Builder. I wrote a function for transforming date from format specified on user PC to YYYY-MM-DD format. I need that part because of API settings.
When I have this function inside my project it works fine, but I need to have that function inside a DLL.
This is how my code looks like:
#pragma hdrstop
#pragma argsused
#include <SysUtils.hpp>
extern DELPHI_PACKAGE void __fastcall DecodeDate(const System::TDateTime DateTime, System::Word &Year, System::Word &Month, System::Word &Day);
extern "C" UnicodeString __declspec (dllexport) __stdcall datum(TDateTime dat) {
Word dan, mjesec, godina;
UnicodeString datum, datum_dan, datum_mjesec, datum_godina;
DecodeDate(dat, godina, mjesec, dan);
if (dan<=9 && mjesec<=9) {
datum_dan="0"+IntToStr(dan);
datum_mjesec="0"+IntToStr(mjesec);
}
if (dan<=9 && mjesec>9) {
datum_dan="0"+IntToStr(dan);
datum_mjesec=IntToStr(mjesec);
}
if (dan>9 && mjesec<=9) {
datum_dan=IntToStr(dan);
datum_mjesec="0"+IntToStr(mjesec);
}
if (dan>9 && mjesec>9) {
datum_dan=IntToStr(dan);
datum_mjesec=IntToStr(mjesec);
}
datum_godina=IntToStr(godina);
return datum_godina+"-"+datum_mjesec+"-"+datum_dan;
}
extern "C" int _libmain(unsigned long reason)
{
return 1;
}
`
I've included SysUtils.hpp and declared DecodeDate() function, without those lines I have a million errors. But with code looking like this, I am getting this error, which I can't get rid of:
[bcc32 Error] File1.cpp(30): E2015 Ambiguity between '_fastcall System::Sysutils::DecodeDate(const System::TDateTime,unsigned short &,unsigned short &,unsigned short &) at c:\program files (x86)\embarcadero\studio\19.0\include\windows\rtl\System.SysUtils.hpp:3466' and '_fastcall DecodeDate(const System::TDateTime,unsigned short &,unsigned short &,unsigned short &) at File1.cpp:25'
Full parser context
File1.cpp(27): parsing: System::UnicodeString __stdcall datum(System::TDateTime)
Can you help me to get rid of that error?
The error message is self-explanatory. You have two functions with the same name in scope, and the compiler doesn't know which one you want to use on line 30 because the parameters you are passing in satisfy both function declarations.
To fix the error, you can change this line:
DecodeDate(dat, godina, mjesec, dan);
To either this:
System::Sysutils::DecodeDate(dat, godina, mjesec, dan);
Or this:
dat.DecodeDate(&godina, &mjesec, &dan);
However, either way, you should get rid of your extern declaration for DecodeDate(), as it doesn't belong in this code at all. You are not implementing DecodeDate() yourself, you are just using the one provided by the RTL. There is already a declaration for DecodeDate() in SysUtils.hpp, which you are #include'ing in your code. That is all the compiler needs.
Just make sure you are linking to the RTL/VCL libraries to resolve the function during the linker stage after compiling. You should have enabled VCL support when you created the DLL project. If you didn't, recreate your project and enable it.
BTW, there is a MUCH easier way to implement your function logic - instead of manually pulling apart the TDateTime and reconstituting its components, just use the SysUtils::FormatDateTime() function or the TDateTime::FormatString() method instead, eg:
UnicodeString __stdcall datum(TDateTime dat)
{
return FormatDateTime(_D("yyyy'-'mm'-'dd"), dat);
}
UnicodeString __stdcall datum(TDateTime dat)
{
return dat.FormatString(_D("yyyy'-'mm'-'dd"));
}
That being said, this code is still wrong, because it is not safe to pass non-POD types, like UnicodeString, over the DLL boundary like you are doing. You need to re-think your DLL function design to use only interop-safe POD types. In this case, change your function to either:
take a wchar_t* as input from the caller, and just fill in the memory block with the desired characters. Let the caller allocate the actual buffer and pass it in to your DLL for populating:
#pragma hdrstop
#pragma argsused
#include <SysUtils.hpp>
extern "C" __declspec(dllexport) int __stdcall datum(double dat, wchar_t *buffer, int buflen)
{
UnicodeString s = FormatDateTime(_D("yyyy'-'mm'-'dd"), dat);
if (!buffer) return s.Length() + 1;
StrLCopy(buffer, s.c_str(), buflen-1);
return StrLen(buffer);
}
extern "C" int _libmain(unsigned long reason)
{
return 1;
}
wchar_t buffer[12] = {};
datum(SomeDateValueHere, buffer, 12);
// use buffer as needed...
int len = datum(SomeDateValueHere, NULL, 0);
wchar_t *buffer = new wchar_t[len];
int len = datum(SomeDateValueHere, buffer, len);
// use buffer as needed...
delete[] buffer;
allocate a wchar_t[] buffer to hold the desired characters, and then return a wchar_t* pointer to that buffer to the caller. Then export a second function that the caller can pass the returned wchar_t* back to you so you can free it correctly.
#pragma hdrstop
#pragma argsused
#include <SysUtils.hpp>
extern "C" __declspec(dllexport) wchar_t* __stdcall datum(double dat)
{
UnicodeString s = FormatDateTime("yyyy'-'mm'-'dd", dat);
wchar_t* buffer = new wchar_t[s.Length()+1];
StrLCopy(buffer, s.c_str(), s.Length());
return buffer;
}
extern "C" __declspec(dllexport) void __stdcall free_datum(wchar_t *dat)
{
delete[] dat;
}
extern "C" int _libmain(unsigned long reason)
{
return 1;
}
wchar_t *buffer = datum(SomeDateValueHere);
// use buffer as needed...
free_datum(buffer);

Weird memory issue with ostringstream / ostream using valgrind

I get this memory issue with valgrind that I cannot make any sense out of. Just adding a line which access the ostream seems to get rid of the memory issue, but that is obviously not the way I want to go. Any ideas what could be wrong? Input to the printBuffer method is a std::ostringstream.
#define FORMATSTRWBUF(pos, buf, len, ...) pos += snprintf(buf + pos, len - pos, __VA_ARGS__)
void printBuffer(std::ostream& os, const char* buffer_name, const unsigned char* buffer, int length) const {
os << buffer_name;
os << "{length ";
os << length;
os << ", contents 0x";
// If this line is here, there is no memory issues, but...
fprintf(stdout, "\n%s %s\n", buffer_name, static_cast<std::ostringstream&>(os).str().c_str());
// fprintf(stdout, "\n%s\n", buffer_name); // having this line only has no effect
int pos = 0;
const int len = 1024;
char buf[len];
for (int32_t i = 0; i < length; ++i) {
FORMATSTRWBUF(pos, buf, len, "%02X", buffer[i]);
}
//... if it is not there is a "Conditional jump or move depends on uninitialised value(s)" memory issue here:
os << buf;
os << "}";
}
==43066== Conditional jump or move depends on uninitialised value(s)
==43066== at 0x4C2C129: strlen (vg_replace_strmem.c:454)
==43066== by 0x5687378: length (char_traits.h:263)
==43066== by 0x5687378: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (ostream:562)
==43066== by 0x44D462: printBuffer(std::ostream&, char const*, unsigned char const*, int) const (message.h:102)
Why do you always seem to find the answer as soon as you have asked a question..
I forgot to initialize buf:
char buf[len] = {0};
did the trick.

ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]?

When I compiled the following program like:
g++ -O2 -s -static 2.cpp it gave me the warning ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result].
But when I remove -02 from copiling statement no warning is shown.
My 2.cpp program:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
return 0;
}
What is the meaning of this warning and what is the meaning of -O2 ??
It means that you do not check the return value of scanf.
It might very well return 1 (only a is set) or 0 (neither a nor b is set).
The reason that it is not shown when compiled without optimization is that the analytics needed to see this is not done unless optimization is enabled. -O2 enables the optimizations - http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html.
Simply checking the return value will remove the warning and make the program behave in a predicable way if it does not receive two numbers:
if( scanf( "%d%d", &a, &b ) != 2 )
{
// do something, like..
fprintf( stderr, "Expected at least two numbers as input\n");
exit(1);
}
I took care of the warning by making an if statement that matches the number of arguments:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int i;
long l;
long long ll;
char ch;
float f;
double d;
//6 arguments expected
if(scanf("%d %ld %lld %c %f %lf", &i, &l, &ll, &ch, &f, &d) == 6)
{
printf("%d\n", i);
printf("%ld\n", l);
printf("%lld\n", ll);
printf("%c\n", ch);
printf("%f\n", f);
printf("%lf\n", d);
}
return 0;
}