[GH-ISSUE #368] Problem compiling images.c example in Visual Studio C++ #293

Closed
opened 2026-05-05 12:06:40 -06:00 by gitea-mirror · 2 comments
Owner

Originally created by @algaspar on GitHub (Apr 10, 2022).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/368

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

Originally created by @algaspar on GitHub (Apr 10, 2022). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/368 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 ```C 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: ```C 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
Author
Owner

@jmcnamara commented on GitHub (Apr 10, 2022):

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};
<!-- gh-comment-id:1094227228 --> @jmcnamara commented on GitHub (Apr 10, 2022): 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: ```C 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}; ```
Author
Owner

@algaspar commented on GitHub (Apr 10, 2022):

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:
@.***>

<!-- gh-comment-id:1094275867 --> @algaspar commented on GitHub (Apr 10, 2022): 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: > ***@***.***> >
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#293
No description provided.