[gambit-list] Visual Studio and C++ FFI

Valery Valery valery at digitalchile.net
Sat Sep 26 07:21:51 EDT 2009


Hello,
I try to compile an example of FFI to C++ under windows.

1) vc2008.bat was copyed to Gambit's root
2) Modifyed one line in vc2008.bat. At the end of "set COMP_GEN=cl -nologo
....." added -D___ENABLE_CPLUSPLUS
3) The bat file executed and finished w/o any issues.
4) Then gsc.exe gsi.exe and gsc-cc-o.bat were copyed to ~/bin folder

My previously tested example of cpp-ffi (wich works fine under linux and
mac) I am compiling with BAT file. Compilation finished with:

cube-test_.obj : error LNK2001: unresolved external symbol
_____20_cube_2d_test
cube-test_.obj : error LNK2019: unresolved external symbol "int __cdecl
___main_char(int,char * * const,union ___mod_or_
lnk_union * (__cdecl*)(struct ___global_state_struct *),char *)"
(?___main_char@@YAHHQAPADP6APAT___mod_or_lnk_union@@PAU
___global_state_struct@@@ZPAD at Z) referenced in function _main
cube-test.exe : fatal error LNK1120: 2 unresolved externals

Whay am I doing wrong?

Content of the source files below. There is also Makefile wich was used at
the unix environments, and make.bat file witch I am using in this case.

---
Valery



==== SOURCE FILES =====

make.bat
-------------------------------------------
del cube-test.exe
del cube-test.c
del cube.o
del cube-test_.c


setlocal
@call "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"

set GAMBCDIR=..\..\Package
set GAMBCDIR_LIB=%GAMBCDIR%\lib
set GAMBCDIR_INCLUDE=%GAMBCDIR%\include

set COMP_GEN=cl -nologo -Oityb1 -Zi -GS -RTC1 -MT -D_CRT_SECURE_NO_DEPRECATE
-c -I%GAMBCDIR_INCLUE% -D___ENABLE_CPLUSPLUS -D___SYS_TYPE_CPU=\"i686\"
-D___SYS_TYPE_VENDOR=\"pc\" -D___SYS_TYPE_OS=\"visualc\"

if not "%1%" == "" (
set COMP_GEN=%COMP_GEN% -D___GAMBCDIR=\"%1%\"
)

set COMP_LIB_MH=%COMP_GEN% -D___LIBRARY
set COMP_LIB_PR_MH=%COMP_LIB_MH% -D___PRIMAL
set COMP_LIB=%COMP_LIB_MH% -D___SINGLE_HOST
set COMP_LIB_PR=%COMP_LIB_PR_MH% -D___SINGLE_HOST
set COMP_APP=%COMP_GEN% -D___SINGLE_HOST

gsc -c cube-test.scm
gsc -link cube-test.c
ren cube-test_.c cube-test_.cpp

%COMP_APP% cube.cpp
%COMP_APP% cube-test_.cpp

cl -Fecube-test.exe %GAMBCDIR_LIB%\libgambc.lib cube-test_.obj cube.obj
Kernel32.Lib User32.Lib Gdi32.Lib WS2_32.Lib


cube.h
-------------------------------------------
#ifndef CUBE_HH
#define CUBE_HH

class Cube
{
public:
Cube();
~Cube();
void setSide(double s);
double getSide();
double Area();
double Volume();
void Properties();
private:
double Side;
};
#endif

cube.cpp
-------------------------------------------
#include <iostream>
#include "cube.h"

Cube::Cube() { }
Cube::~Cube() { }
void Cube::setSide(double s) { Side = s <= 0 ? 1 : s; }
double Cube::getSide() { return Side; }
double Cube::Area() {  return 6 * Side * Side; }
double Cube::Volume() { return Side * Side * Side; }
void Cube::Properties()
{
std::cout << "Characteristics of this cube";
std::cout << "\nSide   = " << getSide();
std::cout << "\nArea   = " << Area();
std::cout << "\nVolume = " << Volume() << "\n\n";
}

cube-test.scm
-------------------------------------------
;;; Gambit should be compiled with  --enable-cplusplus flag

(c-declare "#include \"cube.h\"")

(define cube-new
  (c-lambda () (pointer "Cube") "___result_voidstar = (void *)(new
Cube());"))

(define cube-set-side
 (c-lambda ((pointer "Cube") double) void "___arg1->setSide(___arg2);"))

(define cube-get-side
  (c-lambda ((pointer "Cube")) double "___arg1->getSide();"))

(define cube-properties
  (c-lambda ((pointer "Cube")) void "___arg1->Properties();"))


(display "C++ test\n")
(define c (cube-new))
(cube-set-side c 10.2)
(display (cube-get-side c))
(print "\n")
(cube-properties c)

Makefile
-------------------------------------------
cube-test: cube-test.c cube.o
gsc -link cube-test.c
gcc -o cube-test cube.o -x c++ cube-test.c cube-test_.c -lm -ldl -lgambc
-lstdc++ -lutil -I/usr/local/Gambit-C/include -L/usr/local/Gambit-C/lib

cube.o: cube.cpp cube.h
gcc -c cube.cpp

cube-test.c: cube-test.scm
rm -f cube-test.c
gsc -c -o cube-test.c cube-test.scm

clean:
rm -f cube-test cube-test.c cube.o cube-test_.c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20090926/66418b87/attachment.htm>


More information about the Gambit-list mailing list