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.
Related
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 trying to compile the following code ([cuShiftOr]) to a linux box. Original code is running on Visual Studio 2013 in Windows.
My strategy is to compile all .cu files like below:
nvcc -std=c++11 -Icpp11-range -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env --cl-version 2013 -maxrregcount=0 --machine 64 --compile -cudart static -DWIN32 -DWIN64 -DNDEBUG -D_CONSOLE -D_MBCS -I$HOME/cuShiftOr/CuShiftOr -I$HOME/cuShiftOr/CuShiftOrBenchmark -I$HOME/cuShiftOr/tinyformat -Xcompiler "-fPIC -fexceptions -ffunction-sections -fdata-sections -fpermissive" -ccbin=/opt/cray/pe/craype/2.5.10/bin/CC -c CuShiftOr/device/hybrid.cu -o objs/CuShiftOr/device/hybrid.cu.o
Likewise for the rest.
At the final stage link everything together by running:
nvcc -std=c++11 -Icpp11-range -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env --cl-version 2013 -maxrregcount=0 --machine 64 -cudart static -DWIN32 -DWIN64 -DNDEBUG -D_CONSOLE -D_MBCS -I$HOME/cuShiftOr/CuShiftOr -I$HOME/cuShiftOr/CuShiftOrBenchmark -I$HOME/cuShiftOr/tinyformat -Xcompiler "-fPIC -std=c++11 -Icpp11-range -fexceptions -ffunction-sections -fdata-sections -fpermissive -Wnon-template-friend" -ccbin=/opt/cray/pe/craype/2.5.10/bin/CC -o CuShiftOrBenchmark.a objs/CuShiftOr/device/hybrid.cu.o objs/CuShiftOr/util/op.cu.o objs/CuShiftOr/device/segment.cu.o objs/CuShiftOr/host.cu.o objs/CuShiftOr/util/timer.cu.o objs/CuShiftOr/util/file.cu.o objs/CuShiftOr/util/generator.cu.o objs/CuShiftOrBenchmark/kernel.cu.o -lm
However that leads me this undefined reference issue:
objs/CuShiftOrBenchmark/kernel.cu.o: In function `void run_benchmark<unsigned int>(std::ostream&, cushiftor::device::Handler<unsigned int>*, long, unsigned int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
tmpxft_00003ea2_00000000-4_kernel.cudafe1.cpp:(.text._Z13run_benchmarkIjEvRSoPN9cushiftor6device7HandlerIT_EEljiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_Z13run_benchmarkIjEvRSoPN9cushiftor6device7HandlerIT_EEljiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x203): undefined reference to `cushiftor::util::op::bit<unsigned int>::size'
collect2: error: ld returned 1 exit status
inside op.cuh it is defined as:
template<typename type>
struct bit {
static const int size = sizeof(type) * 8;
};
template<typename word>
__host__ __device__ __forceinline__ word ones(int m) {
return ~word(0) >> (bit<word>::size - m);
}
and then on kernel.cu we have:
#include "util/op.cuh"
using cushiftor::util::op::bit;
using cushiftor::util::op::bytepack;
using cushiftor::util::op::div_up;
using cushiftor::util::op::round_up;
using cushiftor::util::op::shuffle_up;
Further more compiling with Visual Studio 2013 (which works since the project was made on that) shows some instantiations that I am not sure if g++ is doing e.g:
2>C:/Users/A/Desktop/cuShiftOr/CuShiftOrBenchmark/kernel.cu(209): warning C4244: 'argument' : conversion from 'int64_t' to 'cushiftor::env::datasize_t', possible loss of data
2> C:/Users/A/Desktop/cuShiftOr/CuShiftOrBenchmark/kernel.cu(230) : see reference to function template instantiation 'void benchmark_with_file<word>(cushiftor::device::Handler<word> *)' being compiled
2> with
2> [
2> word=unsigned int
2> ]
2>C:/Users/A/Desktop/cuShiftOr/CuShiftOrBenchmark/kernel.cu(86): warning C4018: '<=' : signed/unsigned mismatch
2> C:/Users/A/Desktop/cuShiftOr/CuShiftOrBenchmark/kernel.cu(132) : see reference to function template instantiation 'void run_benchmark<word>(std::ostream &,cushiftor::device::Handler<word> *,int64_t,cushiftor::env::datasize_t,int,std::string)' being compiled
2> with
2> [
2> word=unsigned int
2> ]
Now my C++11 template knowledge is very limited but I did try to instantiate also inside the kernel.cu by adding:
template class cushiftor::util::op::bit<unsigned int>;
However to no avail... Any tips are very welcomed!
I believe this question is essentially a duplicate of this one. However simply marking it as such with no explanation may be a bit obscure. Therefore I'm going to provide a CW answer in the hopes that someone may improve it or correct me if I am wrong.
A possible MCVE derived from the code is as follows (note that I'm converting to using g++ directly rather than nvcc, as the manifestation and workarounds are the same):
$ cat test.cpp
#include <iostream>
namespace cushiftor {
namespace util {
namespace op {
template<typename type>
struct bit {
static const int size = sizeof(type) * 8;
};
}
}
}
#ifdef FIX2
void f(const int data){
#else
void f(const int &data){
#endif
std::cout << "size of type in bits is: " << data << std::endl;
}
using cushiftor::util::op::bit;
#ifdef FIX1
template <typename T> const int bit<T>::size;
#endif
template<typename word>
void run_benchmark() {
f(bit<word>::size);
}
int main(){
run_benchmark<unsigned>();
}
$ g++ -o test test.cpp
/tmp/ccCW51e3.o: In function `void run_benchmark<unsigned int>()':
test.cpp:(.text._Z13run_benchmarkIjEvv[_Z13run_benchmarkIjEvv]+0x5): undefined reference to `cushiftor::util::op::bit<unsigned int>::size'
collect2: error: ld returned 1 exit status
$ g++ -DFIX1 -o test test.cpp
$ g++ -DFIX2 -o test test.cpp
$
This MCVE was created based on inspection of relevant code in kernel.cu, tinyformat.h, and op.cuh, in the original cuShiftOr project (and paper).
We see that if we compile the code as-is, it generates a similar error to that reported in the question.
Compiling with -DFIX2 demonstrates that taking the item in question (bit::size) as a reference argument is essential to witnessing of the issue.
Compiling with -DFIX1 eliminates the issue, and I believe is the correct approach in this case.
The explanation for this issue seems to be that the compiler may treat the static const member as a compile-time constant, unless it is "used" in the code. ("Used" has a special language-specific meaning here.) If it is "used" in the code, then the compiler must be able to take the address of it (sensible in the case of a reference parameter) and to take the address of it, the class/struct definition is not sufficient. (It is apparently only a declaration even though it appears in the class/struct definition. The declaration is sufficient for usage equivalently to a compile-time constant, but is not sufficient for the case where we wish to take the address of it.)
From here:
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19) In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program
I believe the FIX1 code modification satisfies the bolded requirement above.
With the above preamble, I believe it is valid to mark this as a duplicate.
With respect to the behavior of the original project on windows, I would conjecture that compilers may have leeway in enforcement of this, and may in fact provide referenceable member symbols even when the program does not explicitly define them.
Having trouble compiling the following C++ code on Windows 7:
#include <boost/asio.hpp>
#include <iostream>
void handler1(const boost::system::error_code &ec)
{
std::cout << "5 s." << std::endl;
}
void handler2(const boost::system::error_code &ec)
{
std::cout << "10 s." << std::endl;
}
int main()
{
boost::asio::io_service io_service;
boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5));
timer1.async_wait(handler1);
boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(10));
timer2.async_wait(handler2);
io_service.run();
}
I have MinGW installed (gcc 4.8.1) in c:\mingw with my PATH set up correctly. I have downloaded boost and declared environment variable BOOST_ROOT to be the path where it resides. I have gone through the bootstrap and b2 procedure for boost. I now try and compile:
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -o main main.cpp
Gives a bunch of error: '::UnregisterWaitEx' has not been declared errors
I then search a bit and see I may need to link boost_system. So:
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -lboost_system -o main main.cpp
Same errors. Thought I'd try specify library path. Did a search for boost_system and found static libs (libboost_system-mgw48-mt-1_55.a) in %BOOST_ROOT%/stage/lib. So
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp
Same errors. So I search again and see others suggesting appending a -D-D_WIN32_WINNT=0x0601. So
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp -D_WIN32_WINNT=0x0601
And the inevitable errors:
c:\mingw\include\mswsock.h:125:20: error: 'WSAPOLLFD' was not declared in this scope
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:36: error: expected primary-expression before ',' token
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:41: error: expected primary-expression before ')' token
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:41: error: expression list treated as compound expression in initializer [-fpermissive]
Where am I going wrong?
I went ahead and rebuilt Boost again with b2 toolset=gcc --build-type=complete. Same thing happened. Finally, after all that, it turned out all I needed was to put the linking at the end of the command:
C:\path\to\sandbox> g++ -D_WIN32_WINNT=0x0601 -I%BOOST_ROOT% -L%BOOST_ROOT%\stage\lib -o boosttest boosttest.cpp -lwsock32 -lws2_32 -lboost_system-mgw48-mt-d-1_55
C:\path\to\sandbox> boosttest.exe
5 s.
10 s.
The -D_WIN32_WINNT was still necessary and, for anyone who has skipped the other comments, I had to patch winsock.h as detailed http://sourceforge.net/p/mingw/bugs/1980/. And remember to put %BOOST_ROOT%\stage\lib in your PATH so Windows can find the dll at runtime.
Arduous
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.
Here is my test program:
#include <stdio.h>
#pragma GCC diagnostic ignored "-Wcast-qual"
static void proc(char *buf)
{
printf("buf=%p\n",buf);
}
int main(int argc,char **argv)
{
const char *cbuf;
char *buf = (char *)cbuf;
proc(buf);
return(0);
}
Here is my compile:
$ g++ -Wcast-qual x.cpp
x.cpp: In function ‘int main(int, char**)’:
x.cpp:13: warning: cast from type ‘const char*’ to type ‘char*’ casts away constness
$
And here is the compile without the -Wcast-qual:
$ g++ x.cpp
$
I've used #pragma GCC diagnostic ignored in other places of my code without problems. Here it is not working. Can somebody tell me why?
It's a compiler bug on the Mac. GCC 4.7.2 on Linux does not have this problem. Neither does clang++. On the Mac you should try to use clang++, not g++.
Apple should update its compiler.