jmcnamara / libxlsxwriter

A C library for creating Excel XLSX files.
https://libxlsxwriter.github.io
Other
1.49k stars 332 forks source link

Problem compiling images.c example in Visual Studio C++ #368

Closed algaspar closed 2 years ago

algaspar commented 2 years ago

I have been using libxlsxwriter successfully in some C++ applications using Visual Studio 2017 with version 1.1.4 from vcpkg.io. Recently I had occasion to try inserting an image. Just inserting an image with

worksheet_insert_image(worksheet, CELL("A1"), "logo.png");

works fine. I wanted to scale my image down slightly, but if I tried any options, I had a problem. I was using this code:

lxw_image_options options2 = { .x_scale = 0.75,.y_scale = 0.75 };
worksheet_insert_image_opt(worksheet, CELL("A1"), "logo.png", &options2);

Thinking I might have been using lxw_image_options and worksheet_insert_image_opt() incorrectly, I grabbed the images.c example from the documentation and tried compiling it in a new project. When I do a build, I get the same compiler errors I got in my code (here's the first batch of errors I got from images.c generated from the first use of lxw_image_options and worksheet_insert_image_opt()). It really doesn't like '.x_scale' and '.y_scale':

1>c:\users\al\source\repos\project9\project9\source.cpp(28): error C2059: syntax error: '.'
1>c:\users\al\source\repos\project9\project9\source.cpp(28): error C2143: syntax error: missing ';' before '}'
1>c:\users\al\source\repos\project9\project9\source.cpp(29): error C2065: 'worksheet': undeclared identifier
1>c:\users\al\source\repos\project9\project9\source.cpp(29): error C2065: 'options1': undeclared identifier
1>c:\users\al\source\repos\project9\project9\source.cpp(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\al\source\repos\project9\project9\source.cpp(29): error C2365: 'worksheet_insert_image_opt': redefinition; previous definition was 'function'
1>c:\src\vcpkg\installed\x64-windows\include\xlsxwriter\worksheet.h(3694): note: see declaration of 'worksheet_insert_image_opt'
1>c:\users\al\source\repos\project9\project9\source.cpp(32): error C2065: 'worksheet': undeclared identifier
1>c:\users\al\source\repos\project9\project9\source.cpp(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\al\source\repos\project9\project9\source.cpp(32): error C2365: 'worksheet_write_string': redefinition; previous definition was 'function'
1>c:\src\vcpkg\installed\x64-windows\include\xlsxwriter\worksheet.h(2458): note: see declaration of 'worksheet_write_string'

This repeats for every instance of lxw_image_options and subsequent call to worksheet_insert_image_opt() in images.c.

Am I doing something wrong or do the image options just not work for C++?

Thanks for any help or suggestions (and thanks for a great library!)--

Al

jmcnamara commented 2 years ago

This is a C vs C++ difference and not a libxlsxwriter issue.

C style field initializers aren't supported in C++. See the various answers and explanations here: https://stackoverflow.com/questions/11516657/c-structure-initialization

Instead you should use C++ compatible initializers, something like this:

lxw_image_options options2 = {0};
options2.x_scale = 0.75;
options2.y_scale = 0.75;

// or 
lxw_image_options options2 = {0, 0, 0.75, 0.75, 0, NULL, 0, NULL, NULL};
algaspar commented 2 years ago

Thank you very much for your quick and detailed reply. I understand what I need to do.

Best--

Al Gaspar

On Sun, Apr 10, 2022, 4:19 AM John McNamara @.***> wrote:

This is a C vs C++ difference and not a libxlsxwriter issue.

C style field initializers aren't supported in C++. See the various answers and explanations here: https://stackoverflow.com/questions/11516657/c-structure-initialization

Instead you should use C++ compatible initializers, something like this:

lxw_image_options options2 = {0}; options2.x_scale = 0.75; options2.y_scale = 0.75; // or lxw_image_options options2 = {0, 0, 0.75, 0.75, 0, NULL, 0, NULL, NULL};

— Reply to this email directly, view it on GitHub https://github.com/jmcnamara/libxlsxwriter/issues/368#issuecomment-1094227228, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALF36ENTCQSL77WVHTRX3YTVEKMLZANCNFSM5TAI3UOA . You are receiving this because you authored the thread.Message ID: @.***>