What does this macro mean in GCC #define guard(n) asm("#" #n)
Ran into some code that uses this macro as guard(1);... guard(2)... etc.
What does this inline assembly do
asm(#1).. asm(#2)..
Related
In the kernel code, I cannot find the definition of the native_queued_spin_lock_slowpath function, __pv_queued_spin_lock_slowpath is the same, where are these functions defined? I searched all the kernel code, but in vain
The definition of native_queued_spin_lock_slowpath is in "kernel/locking/qspinlock.c", using a macro to change the name of queued_spin_lock_slowpath to native_queued_spin_lock_slowpath when CONFIG_PARAVIRT_SPINLOCKS is defined:
#ifdef CONFIG_PARAVIRT_SPINLOCKS
#define queued_spin_lock_slowpath native_queued_spin_lock_slowpath
#endif
…
void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
{
The definition of __pv_queued_spin_lock_slowpath is also in "kernel/locking/qspinlock.c" using the same renaming macro trick with a twist — The "qspinlock.c" file includes itself once more, using a guard macro _GEN_PV_LOCK_SLOWPATH to avoid infinite recursive inclusion:
#if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS)
#define _GEN_PV_LOCK_SLOWPATH
…
#undef queued_spin_lock_slowpath
#define queued_spin_lock_slowpath __pv_queued_spin_lock_slowpath
#include "qspinlock_paravirt.h"
#include "qspinlock.c"
…
#endif
I want to compile a simple test project for a library that makes use of a ftd2xx driver. I already compiled it successfully on linux and I'm trying to do the same on Windows. The main difference are some minor modification to the library.
The test file I want to compile is this:
//#include "HPX-linux.h"
#include "HPX-Windows.h"
#include <stdlib.h>
#include <stdio.h>
int main(){
int devs;
getSerialNum(&devs);
printf("%d\n\n", devs);
simpleTest("./myTest/");
return 0;
}
And the preprocessing directives of HPX-Windows.h are as follows:
#ifndef HPXLINUX_H
#define HPXLINUX_H
#include <math.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include "ftd2xx.h"
#include <pthread.h>
// typedefs
//typedef uint16_t DWORD;
#ifndef __cplusplus
typedef uint8_t bool;
#endif
// static const defines
#ifndef __cplusplus
#define TRUE 1
#define FALSE 0
#endif
#define SUCCESS 0
#define FAILURE -1
#define RETRIEDTOOMANY -10
#define LOSTHEADFRAME -11
#define GOTAV 2
#ifdef __unix__
#define PRELIB extern
#elif _WIN32
#ifdef ADD_EXPORTS
#define PRELIB __declspec(dllexport)
#else
#define PRELIB __declspec(dllimport)
#endif
#endif
#ifdef __unix__
#define CALL
#elif _WIN32
#define CALL __cdecl
#endif
About ftd2xx, I have 2 .h headers, a .lib and a .dll.
With the driver properly installed, I could compile the library on linux with:
gcc -o test test.c -Wall -Wextra -lHPX-linux -lftd2xx -lm
I'm using MinGW on Windows. The command I'm using is:
gcc test.c HPX-Windows.c -L -lftd2xx -g
And then I get a list of errors type "undefined reference to _imp__*", being * a function. I expected them to be the functions of ftd2xx.h, but it also happens to function declared in HPX-Windows.h, including getSerialNum and simpleTest. Why does it happen when I'm using a .c source file instead of a library?
The error is caused by symbols exported with __declspec(dllimport) in header files that can't be imported from a shared library (.DLL).
I would recommend creating a libftd2xx.dll.a file from the ftd2xx.dll file and linking with that (e.g. if the files are in the current directory using -L. -lftd2xx.dll).
Or you could probably just link with the .dll file by specifying it in the gcc command, something like this: gcc -o test.exe test.c HPX-Windows.c ftd2xx.dll -g).
If that doesn't work check ftd2xx.h to see where __declspec(dllimport) is imported and see if you can set a define that causes the header to not use __declspec(dllexport)/__declspec(dllimport) and link with your lib file in case it's a static library (something like: gcc --static -o test.exe test.c HPX-Windows.c -L. -lftd2xx -g).
I am attempting to create a wrapper with swig for a static library (*.a) to consume with Golang.
I am using a mylib.swigcxx file that triggers Golang to call swig with the following options:
swig -go -cgo -intgosize 64 -module mylib \
-o $WORK/mylib/_obj/mylib_wrap.cxx \
-outdir $WORK/mylib/_obj/ \
-I/mylib/lib \
-c++ mylib.swigcxx
The mylib.swigcxx looks like:
%module mylib
%{
// (snip)
#include "basic/internal/config.h"
#include "basic/internal/config_autogen.h"
// (snip)
%}
%include <typemaps.i>
%include "std_string.i"
%include "std_vector.i"
// This will create 2 wrapped types in Go called
// "StringVector" and "ByteVector" for their respective
// types.
namespace std {
%template(StringVector) vector<string>;
%template(ByteVector) vector<char>;
}
// ...
%include "basic/internal/config.h"
%include "basic/internal/config_autogen.h"
// ...
The large library I am attempting to wrap has a header file with the following code:
70 // C++11 standard
71
72 #if __cplusplus < 201103
73
74 #if defined(_MSC_VER)
75 #if _MSC_VER < 1700
76 #error "Compiling OGDF requires Visual C++ 11 (Visual Studio 2012) or higher!"
77 #endif
78
79 #elif defined(__GNUC__)
80 #ifndef __GXX_EXPERIMENTAL_CXX0X__
81 #error "No C++11 support activated for g++ (compile with -std=c++0x or -std=c++11)!"
82 #endif
83
84 #else
85 #error "Compiling OGDF requires a C++11 compliant compiler!"
86 #endif
87
88 #endif
89
As you can see, it is checking if __cplusplus is set correctly and if not to display an error.
The errors I get during swig execution is:
/mylib/lib -c++ mylib.swigcxx
/mylib/lib/basic/internal/config.h:85: Error: CPP #error ""Compiling OGDF requires a C++11 compliant compiler!"". Use the -cpperraswarn option to continue swig processing.
/mylib/lib/basic/internal/config.h:254: Error: CPP #error ""OGDF is compiled without LP solver. Check your build configuration."". Use the -cpperraswarn option to continue swig processing.
/mylib/lib/basic/internal/config.h:299: Error: CPP #error ""OGDF is compiled without memory manager. Check your build configuration."". Use the -cpperraswarn option to continue swig processing.
As you can see, it's failing on line 85 for __cplusplus < 201103 and the else clause.
According to the SWIG 3.0 docs, this should be set with the -c++ flag:
__cplusplus Defined when -c++ option used
Any help would be appreciated.
I suspect that __cplusplus is defined, but its value is less than 201103.
Adding the following inside your .i file ought to solve this if the C++11 features your library needs are actually supported by SWIG.
#undef __cplusplus
#define __cplusplus 201103
Why am I not able to compile my code to c++ 11 and use the srand48 function?
I have a program where I play around with some matrices.
The problem is that when I compile the code with the -std=c++0x flag.
I want to use some c++11 only functions and this is my approach to do so.
It compiles without any problems if I do not specify the c++ version. Like this:
g++ -O2 -Wall test.cpp -o test -g
Please correct me if I have misunderstood what the mentioned flag does.
I run my code on a Windows 7 64-bit machine and compile through cygwin. I use g++ version 4.5.3 (GCC). Please comment if more information is required.
For some unknown reason (even to myself) then all my code is written in one compilation unit.
If the error is caused by a structural error then you should also feel free to point it out. :)
I receive the following errors:
g++ -std=c++0x -O2 -Wall test.cpp -o test -g
test.cpp: In function ‘void gen_mat(T*, size_t)’:
test.cpp:28:16: error: there are no arguments to ‘srand48’ that depend on a template parameter, so a declaration of ‘srand48’ must be available
test.cpp:28:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test.cpp:33:28: error: there are no arguments to ‘drand48’ that depend on a template parameter, so a declaration of ‘drand48’ must be available
Here is a sub of my code, it generates the errors shown above.
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <limits.h>
#include <math.h>
#define RANGE(S) (S)
// Precision for checking identity.
#define PRECISION 1e-10
using namespace std;
template <typename T>
void gen_mat(T *a, size_t dim)
{
srand48(dim);
for(size_t i = 0; i < dim; ++i)
{
for(size_t j = 0; j < dim; ++j)
{
T z = (drand48() - 0.5)*RANGE(dim);
a[i*dim+j] = (z < 10*PRECISION && z > -10*PRECISION) ? 0.0 : z;
}
}
}
int main(int argc, char *argv[])
{
}
Regards Kim.
This is the solution that solved the problem for me:
First n.m. explained that srand() can not be used when compiling with -std=c++0x.
The correct flag to use is -std=gnu++11 however it require g++ version 4.7+
Therefore, the solution for me was to compile my code with -std=gnu++0x
The compile command = g++ -O2 -Wall test.cpp -o test -g -std=gnu++0x
If you explicitly set -stc=c++03 you will get the same error. This is because drand48 and friends are not actually a part of any C++ standard. gcc includes these functions as an extension, and disables them if standard behaviour is requested.
The default standard mode of g++ is actually -std=gnu++03. You may want to use -std=gnu++11 instead of -std=c++0x, or pass -U__STRICT_ANSI__ to the compiler.
I have application for Windows Mobile 6. To build it I use cegcc (arm-mingw32ce).
Now I have new device with new SDK and some functions from this SDK must be added to this application.
Here comes SDK.
header.h
#ifndef _HEADER_H_
#define _HEADER_H_
#include <windows.h>
#ifndef _SOME_FLAG_
extern "C"
{
#endif
BOOL foo(DWORD *lpdwParam1, DWORD *lpdwParam2);
#ifndef _SOME_FLAG_
}
#endif
#endif
library.lib (this is probably import library compiled with VC++, there is library.dll on device)
Some output from "dumpbin /all library.lib"
2DA0 ?foo##YAHPAK0#Z
2DA0 __imp_?foo##YAHPAK0#Z
Archive member name at 2DA0: library.dll/
correct header end
Version : 0
Machine : 1C2 (Thumb)
SizeOfData : 0000002B
DLL name : library.dll
Symbol name : ?foo##YAHPAK0#Z (int __cdecl foo(unsigned long *,unsigned long *))
Type : code
Name type : undecorate
Hint : 14
Name : foo
I'm able to use this SDK in VS2k5 (installation of Windows Mobile SDK was needed...) but compiling with cegcc fail.
I was trying to compile and link it as C and Cpp. With and without _SOME_FLAG_ defined (C compilation with this flag set fail on extern "C" of course).
The results are:
undefined reference to `foo'
when C compiled or Cpp compiled with extern "C" and
undefined reference to `foo(unsigned long*, unsigned long*)'
when Cpp compiled without extern "C".
Compile:
gcc -O2 -Wall -Wextra -pedantic -Wno-long-long -g -c -DUNICODE -D_UNICODE -Ic:\inc sample.c
Linking:
gcc -static -mconsole -o sample obj\sample.o -lc:\lib\library.lib -laygshell
When I Cpp compile I'm only changing sample.c to sample.cpp (there is only main with simple foo call).
It look like there is a mangling problem (vc++ vs gcc). I've tried to add __attribute__((dllimport)) and __attribute__((cdecl))
How can I solve this problem? Any ideas?
Problem solved. I've forget about possibility of run-time dynamic linking
#include <windows.h>
#include <winbase.h>
#include <header.h>
HINSTANCE dllinst = NULL;
typedef BOOL (CALLBACK LP_FOO)(DWORD *lpdwParam1, DWORD *lpdwParam2);
static LP_FOO Foo;
dllinst = LoadLibrary("library.dll");
Foo = (LP_FOO) GetProcAddress((HMODULE) dllinst, "foo");
Now I can use Foo same as foo.