Flex how to avoid using errno - windows-ce

I am using Flex&Bison in winCE 6.0.
There are some problem in errno.h.
In my SDK the errno.h is:
#ifndef _INC_ERRNO
#define _INC_ERRNO
#include <crtdefs.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Error Codes */
But the SDK of VC which also used in Flex is:
#ifndef _INC_ERRNO
#define _INC_ERRNO
#include <crtdefs.h>
#ifdef __cplusplus
extern "C" {
#endif
/* declare reference to errno */
#ifndef _CRT_ERRNO_DEFINED
#define _CRT_ERRNO_DEFINED
_CRTIMP extern int * __cdecl _errno(void);
#define errno (*_errno())
errno_t __cdecl _set_errno(__in int _Value);
errno_t __cdecl _get_errno(__out int * _Value);
#endif
/* Error Codes */
So,is it possible to avoid using errno in Flex?
Add some option? or privode myself header file instead of errno.h?

Related

Rust compile C/Cuda

I have an existing project in Rust / C and I want to migrate some low level hashing stuff to CUDA, but I can't get it to finish compiling.
I believe the compile part is working, as the error only appears in the linker if I call the function from the .cu file
build.rs
fn main() {
let mut cfg = cc::Build::new();
cfg.cuda(true);
cfg.include("project/include")
.include("project/src")
.file("project/src/HelloWorld.cu")
.file("project/src/Validate.c")
//more C files...
.out_dir(dst.join("lib"))
.flag("-O2")
.compile("libproject.a");
println!("cargo:root={}", dst.display());
println!("cargo:include={}", dst.join("include").display());
println!(
"cargo:rerun-if-changed={}",
env::current_dir().unwrap().to_string_lossy()
);
println!("cargo:rerun-if-env-changed=PC_CC");
if let Ok(cuda_path) = env::var("CUDA_HOME") {
println!("cargo:rustc-link-search=native={}/lib64", cuda_path);
} else {
println!("cargo:rustc-link-search=native=/usr/local/cuda/lib64");
}
println!("cargo:rustc-link-lib=dylib=cudart");
}
HelloWorld.h
#ifndef CUDA_HELLO_WORLD_H
#define CUDA_HELLO_WORLD_H
#include <stdio.h>
#include "cuda_runtime.h"
void cudaTest();
#endif
HelloWorld.cu
#include "HelloWorld.h"
__global__ void mykernel(void){
}
void cudaTest(){
mykernel<<<1,1>>>();
printf("Hello World!\n");
}
error:
error: linking with `cc` failed: exit status: 1
[...] really big compile command
= note: /usr/bin/ld: project/target/debug/deps/libproject-673a2f9d363593e3.rlib(File.o): in function `call_to_cuda_file`:
project/project/src/File.c:168: undefined reference to `cudaTest`
collect2: error: ld returned 1 exit status
There was a linking problem as CUDA uses C++ linkage
The solution was to modify HelloWorld.h to
#ifndef CUDA_HELLO_WORLD_H
#define CUDA_HELLO_WORLD_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include "cuda_runtime.h"
void cudaTest();
#ifdef __cplusplus
}
#endif
#endif
There was no need to modify anything on HelloWorld.cu

How to use the macro SCHED_DEADLINE in linux?

We know that there are several scheduling policies in linux like SCHED_FIFO, SCHED_RR, SCHED_OTHER, etc. and one can change the scheduler of a real-time process using the sched_setscheduler system call.
But I'm not able to change the scheduler of a program to Earliest-deadline-first using the SCHED_DEADLINE macro ? Can anyone suggest a way how to achieve this ?
This link has example code for EDF algorithm ie deadline scheduling.
http://www.admin-magazine.com/Archive/2015/25/Optimizing-utilization-with-the-EDF-scheduler
sched_setattr() has to be used for deadline scheduling, instead of sched_setscheduler() which can be used to invoke sched_rr/sched_fifo/sched_other....etc. Also, the period of threads must be compatible enough to accommodate the threads' periods otherwise the sched_setattr returns error.
First of all, you need a 3.14+ Linux kernel.
Moreover, since glibc does not yet provide the API wrapping the new scheduler syscall (i.e. ) you need to wrap them by yourself:
#define _GNU_SOURCE
#include <linux/kernel.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <time.h>
#include <linux/types.h>
#include <sched.h>
#include <linux/sched.h>
#include <sys/types.h>
#define SCHED_DEADLINE 6
/* __NR_sched_setattr number */
#ifndef __NR_sched_setattr
#ifdef __x86_64__
#define __NR_sched_setattr 314
#endif
#ifdef __i386__
#define __NR_sched_setattr 351
#endif
#ifdef __arm__
#define __NR_sched_setattr 380
#endif
#ifdef __aarch64__
#define __NR_sched_setattr 274
#endif
#endif
/* __NR_sched_getattr number */
#ifndef __NR_sched_getattr
#ifdef __x86_64__
#define __NR_sched_getattr 315
#endif
#ifdef __i386__
#define __NR_sched_getattr 352
#endif
#ifdef __arm__
#define __NR_sched_getattr 381
#endif
#ifdef __aarch64__
#define __NR_sched_getattr 275
#endif
#endif
struct sched_attr {
__u32 size;
__u32 sched_policy;
__u64 sched_flags;
/* SCHED_NORMAL, SCHED_BATCH */
__s32 sched_nice;
/* SCHED_FIFO, SCHED_RR */
__u32 sched_priority;
/* SCHED_DEADLINE */
__u64 sched_runtime;
__u64 sched_deadline;
__u64 sched_period;
};
int sched_setattr(pid_t pid,
const struct sched_attr *attr,
unsigned int flags)
{
return syscall(__NR_sched_setattr, pid, attr, flags);
}
int sched_getattr(pid_t pid,
struct sched_attr *attr,
unsigned int size,
unsigned int flags)
{
return syscall(__NR_sched_getattr, pid, attr, size, flags);
}

VS 2010 express. Syntax error : 'do'

I am working with code that was written in VS 6.0. I am able to open the project in VS 2010 express. But when I compile it, it gives me the odd errors:
here the code that it's pointing to
#define AFX_CRT_ERRORCHECK(expr) do { expr; } while (0)
#if _SECURE_ATL
#ifdef _AFX
#define ATLMFC_CRT_ERRORCHECK(expr) AFX_CRT_ERRORCHECK(expr)
#else
#define ATLMFC_CRT_ERRORCHECK(expr) ATL_CRT_ERRORCHECK(expr)
#endif
...
inline errno_t __cdecl strncpy_s(__out_ecount(_SizeInChars) char *_Dest, __in size_t _SizeInChars, __in_z const char *_Source, __in size_t _Count)
{
return ATLMFC_CRT_ERRORCHECK(::strncpy_s(_Dest, _SizeInChars, _Source,_Count));
}
inline errno_t __cdecl wcsncpy_s(__out_ecount(_SizeInChars) wchar_t *_Dest, __in size_t _SizeInChars, __in_z const wchar_t *_Source, __in size_t _Count)
{
return ATLMFC_CRT_ERRORCHECK(::wcsncpy_s(_Dest, _SizeInChars, _Source,_Count));
}
inline errno_t __cdecl tcsncpy_s(__out_ecount(_SizeInChars) TCHAR *_Dest, __in size_t _SizeInChars, __in_z const TCHAR *_Source, __in size_t _Count)
{
#ifndef _ATL_MIN_CRT
return ATLMFC_CRT_ERRORCHECK(::_tcsncpy_s(_Dest, _SizeInChars, _Source,_Count));
#else
#ifdef UNICODE
return ATLMFC_CRT_ERRORCHECK(::wcsncpy_s(_Dest, _SizeInChars, _Source,_Count));
#else
return ATLMFC_CRT_ERRORCHECK(::strncpy_s(_Dest, _SizeInChars, _Source,_Count));
#endif
#endif
}

Cannot use VS2008 memory leak detector

I am trying to use VS2008 memory leak tool, but I have failed to build it at all.
The simplest scenarios works well, but when I try to use CObject - it does not compile
Here is the code (Its a newly create console application)
#include "stdafx.h"
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#define _AFXDLL
#include "afx.h"
class DRV : public CObject {};
int _tmain(int argc, _TCHAR* argv[])
{
DRV *d = new DRV;
}
This results : error C2059: syntax error : 'constant' in afx.h:
void* PASCAL operator new(size_t nSize);
If I try to move the #ifdef _DEBUG below the #include "afx.h", I get:
error C2661: 'CObject::operator new' : no overloaded function takes 4 arguments
on line:
DRV *d = new DRV;
So - what am I doing wrong?
Can I use the build in VS2008 memory leak detector?
Please help
Create file DebugNew.h and add this code to it:
#pragma once
#include "crtdbg.h"
#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW
#endif
In cpp file:
#include "stdafx.h"
#include "DebugNew.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int _tmain(int argc, _TCHAR* argv[])
{
CrtSetDbgFlag( _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
char *d = new char[100];
}
DebugNew.h file defines new operator, which allows to include source line information for every allocation. #define new DEBUG_NEW line redefines default new to DEBUG_NEW, only in Debug build. This line should be placed after all #include lines in all .cpp files. CrtSetDbgFlag enables memory leak allocation in debug build - when the program exits, all unreleased allocations are printed. Since new operator is redefined, they are printed with source line information.
For MFC projects, you only need to add lines
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
to every .cpp file. All other things are already done by MFC. MFC project, created by MFC application Wizard, already contains all required stuff by default. For example, create Win32 Console Application with MFC support using Wizard - memory leaks tracking is working. You only need to add new DEBUG_NEW redefinition to every new file added to the project.

MSVC 10.0 c vs c++ differences

I'm having difficulty compiling the C program below, It's just the begining of me trying to understand winsock.
The issue is that when compiling the program client.c, I get an error (C2143) missing ';' before 'type'
But when I re-name the source file to 'client.cpp' the program compiles with no errors or warnings.
I don't understand the syntax error that is an error in C but not C++.
#define WIN32_LEAN_AND_MEAN
#define DEBUG
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define PORT "12186"
#define BUFFERLEN 512
int main(int argc, char* argv[])
{
/*
Variable Declorations
*/
WSADATA wsaData;
SOCKET ConnectionSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, *ptr = NULL, hints;
int addrResult;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC; //unspecified so we can be compatible with IPv4 and IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
#ifdef DEBUG
printf("IPPROTO_TCP: %d", IPPROTO_TCP);
#endif
//Buffers
char * sendbuffer; // Error C2143
char recievebuffer [BUFFERLEN]; //Error C2143
//Initialize Winsock
addrResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(addrResult !=0)
{
printf("WSAStartup failed: %d", addrResult);
}
addrResult = getaddrinfo(argv[1], PORT, &hints, &result);
if(addrResult != 0)
{
printf("getaddrinfo failed: %d", addrResult);
WSACleanup();
return 1;
}
return 0;
}
Edit:
C variable declorations have to go before all other code in MSVC C functions.
Problem solved.
Is this a C89 thing or is it just MSVC?
The problem might be the place where variable declarations are going. Place them at the beginning of the function with the other variables.
See the last example from MSDN that can cause this error code.
The C compiler which ships with VS only implements C89 (seriously...), so you must declare all of your variables at the top of a given function.

Resources