Compiles fine with g++ but fails with Visual Studio 2015 - visual-c++
I have a perplexing problem with Visual Studio 2015, C++, macros. I do have a bigger project at the background, but the following toy example illustrates the problem.
I have the following C++ code (oma8.rc, oma8.h, oma8.cpp) to create a toy dialog. It compiles with gcc compiler nicely:
windres -O coff -i oma8.rc -o oma8.res
g++ oma8.cpp oma8.res -o oma8
// oma8.cpp
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include "stdlib.h"
#include "oma8.h"
using namespace std;
string param2;
int b1, b3;
string GetInputText( HWND dlg, int resid ) {
HWND hc = GetDlgItem( dlg, resid );
int n = GetWindowTextLength( hc ) + 1;
string s( n, 0 );
GetWindowText( hc, &s[0], n );
return s;
}
int GetCheck( HWND dlg, int resid) {
HWND hc = GetDlgItem(dlg,resid);
int n = IsDlgButtonChecked( dlg, resid);
return n;
}
BOOL CALLBACK DialogProc( HWND hwnd, UINT message,
WPARAM wp, LPARAM ) {
switch ( message ) {
case WM_COMMAND: {
int ctrl = LOWORD( wp );
int event = HIWORD( wp );
if ( ctrl == IDCANCEL && event == BN_CLICKED ) {
DestroyWindow (hwnd);
return TRUE;
}
else if ( ctrl == IDOK && event == BN_CLICKED ) {
param2 = GetInputText( hwnd, IDC_EDIT1 );
b1 = GetCheck( hwnd, RB1 );
b3 = GetCheck( hwnd, RB3 ); // ignore the other buttons for now
DestroyWindow (hwnd);
return TRUE;
}
return FALSE;
}
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
case WM_CLOSE:
DestroyWindow (hwnd);
return TRUE;
}
return FALSE;
}
int main() {
HWND dlg = CreateDialog( GetModuleHandle(0),
MAKEINTRESOURCE( IDD_DIALOG1 ),
0, DialogProc );
MSG msg;
while ( GetMessage( & msg, 0, 0, 0 ) ) {
if ( ! IsDialogMessage( dlg, & msg ) ) {
TranslateMessage( & msg );
DispatchMessage( & msg );
}
}
cout << "corner text: " << param2 << " as a number: " << atof(¶m2[0]) << endl;
cout << "radio1: " << b1 << " radio3: " << b3 << endl;
}
// oma8.rc (stripped 3 comment lines from start)
#include <windows.h>
#include "oma8.h"
//#include "winresrc.h" // apparently not needed
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 20, 20, TABNR(8), THISLINEHEIGHT(15)
STYLE DS_3DLOOK | DS_SETFONT | WS_CAPTION | WS_SYSMENU | WS_VISIBLE | WS_SIZEBOX
//STYLE DS_CENTER | DS_SHELLFONT | DS_FIXEDSYS | DS_MODALFRAME | WS_VSCROLL | WS_POPUP
CAPTION "Caption here"
FONT 10, "Arial"
{
LTEXT "Corner text",IDC_STATIC,LMARG,UPMARG,60,RHEIGHT
EDITTEXT IDC_EDIT1, 60, UPMARG, 60,FIELDH, ES_AUTOHSCROLL
DEFPUSHBUTTON "OK",IDOK,TABNR(5),UPMARG,TABW,FIELDH
PUSHBUTTON "Cancel",IDCANCEL,TABNR(6),UPMARG,TABW,FIELDH
LTEXT "Block1, line 1",IDC_STATIC,TABNR(RBCOLUMN),THISLINEHEIGHT(PARAMLINE),TEXTW,RHEIGHT
AUTORADIOBUTTON "B1, line 2",RB1,TABNR(RBCOLUMN),THISLINEHEIGHT(PARAMLINE+1),TEXTW,RHEIGHT,WS_GROUP
AUTORADIOBUTTON "B1, line 3",RB2,TABNR(RBCOLUMN),THISLINEHEIGHT(PARAMLINE+2),TEXTW,RHEIGHT
LTEXT "Block2 col 1",IDC_STATIC,TABNR(SNGLCOLUMN),THISLINEHEIGHT(CHOICELINE),TABW,RHEIGHT
AUTORADIOBUTTON "B2 col 2",RB3,TABNR(SNGLCOLUMN+1),THISLINEHEIGHT(CHOICELINE),TABW,RHEIGHT, WS_GROUP
AUTORADIOBUTTON "B2 col 3",RB4,TABNR(SNGLCOLUMN+2),THISLINEHEIGHT(CHOICELINE),TABW,RHEIGHT
LTEXT "Block3, line 1",IDC_STATIC,TABNR(RANGECOLUMN),THISLINEHEIGHT(TXLINE),TEXTW,RHEIGHT
AUTORADIOBUTTON "B3, line 2",RB5,TABNR(RANGECOLUMN),THISLINEHEIGHT(RXLINE),TEXTW,RHEIGHT,WS_GROUP
AUTORADIOBUTTON "B3, line 3",RB6,TABNR(RANGECOLUMN),THISLINEHEIGHT(SCLINE),TEXTW,RHEIGHT
}
// oma8.h (stripped 2 comment lines from start)
#define IDD_DIALOG1 100
// basic buttons
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#ifndef IDOK
#define IDOK 40000
#endif
#ifndef IDCANCEL
#define IDCANCEL 40001
#endif
// measures
#define LMARG 6
#define UPMARG 5
#define TEXTW 120 // rb text width
#define TABW 50 // tab width
#define FIELDW 30 // fill-in field w
#define FIELDH 13 // fill-in field h
#define RHEIGHT 14 // row height
// line numbers
#define PARAMLINE 2
#define NOFPARAMS 2
#define OFFSET2 2
#define OFFSET3 5
#define CHOICELINE (PARAMLINE+NOFPARAMS+OFFSET2)
#define TXLINE (CHOICELINE+1)
#define RXLINE (CHOICELINE+2)
#define SCLINE (CHOICELINE+3)
#define RANGETITLELINE 2
#define RANGEFIELDLINE (RANGETITLELINE+1)
#define DETECTORLINE (CHOICELINE+OFFSET3)
#define THISLINEHEIGHT(x) (UPMARG+(x)*RHEIGHT)
// column numbers
#define TABNR(x) (LMARG+TABW*(x))
#define RBCOLUMN 1
#define RANGECOLUMN 5
#define SNGLCOLUMN 2
#define DTCTCOLUMN 7
// parameter buttons
#define IDC_EDIT1 1002
#define RB1 1010
#define RB2 1011
#define RB3 1012
#define RB4 1013
#define RB5 1014
#define RB6 1015
and oma8.exe (all commands given in a console) opens a dialog that looks exactly as I want it to, hooray. But, I have reasons that I must compile this also with VS2015. (It will be later inserted into a certain boilerplate code, but that's not relevant here.)
When trying to compile the same code with VS2015, I get a bunch of error messages related to the .rc file, especially on its macro functions. The first 4 are
Error RC2112 BEGIN expected in dialog oma8.rc 11
Error RC2135 file not found: 0x00040000L oma8.rc 12
Error RC2135 file not found: here oma8.rc 14
Error RC2135 file not found: 10 oma8.rc 15
Furthermore, if I change the lines
#define THISLINEHEIGHT(x) (UPMARG+(x)*RHEIGHT)
#define TABNR(x) (LMARG+TABW*(x))
into
#define THISLINEHEIGHT(x) (UPMARG + (x) * RHEIGHT)
#define TABNR(x) (LMARG + TABW * (x))
I get just one error message:
Error RC2104 undefined keyword or key name: * oma8.rc 11
I must have something funny with my linker/compiler options? When creating this as a Project From Existing Code, I chose Console App and left all other choices as defaults. (Can't remember what those were, sorry, just lots of acronyms.)
I've spent over a week googling and trying different modifications, but haven't been able to fix this. I don't know what else to try. Where should I look into? Any ideas and further questions would be welcome.
My system is Win7, 64-bit. g++ version says g++ (tdm-1) 5.1.0 (C) 2015 Free Software Foundation Inc.
Related
How to edit ELF files in Linux?
I have written two similar C programs. How can I make the outputs of both code same by editing one of the ELF files not the actual code? /** * prg1.c */ #include <stdio.h> #include <stdlib.h> int main() { int a = 5; int b = 10; int sum; sum = a + b; printf("sum is %d\n", sum); return(0); } /** * prg2.c */ #include <stdio.h> #include <stdlib.h> int main() { int a = 5; int b = 20; int sum; sum = a + b; printf("sum is %d\n", sum); return(0); }
In your second program's elf file find the occurrence of 20 and change it to 10. To do that you can do something like this - Find 14 (hex of 20) in your elf file and change it to A and making sure length is same by adding extra 0. To do this you can use any elf editor, I use 'Hex Fiend' for mac.
I'm trying to create a string with n characters by allocating memories with malloc, but I have a problem
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main(void) { int n; printf("Length? "); scanf("%d", &n); getchar(); char* str = (char*)malloc(sizeof(char) * (n+1)); fgets(str,sizeof(str),stdin); for (int i = 0; i < n; i++) printf("%c\n", str[i]); free(str); } Process results like this! Length? 5 abcde a b c ? (I wanted to upload the result image, but I got rejected since I didn't have 10 reputations) I can't figure out why 'd' and 'e' won't be showing in the results. What is the problem with my code??
(wellcome to stackoverflow :) (update #1) str is a pointer to char instead of a character array therefore sizeof(str) is always 8 on 64-bit or 4 on 32-bit machines, no matter how much space you have allocated. Demo (compilation succeeds only if X in static_assert(X) holds): #include <assert.h> #include <stdlib.h> int main(void){ // Pointer to char char *str=(char*)malloc(1024); #if defined _WIN64 || defined __x86_64__ || defined _____LP64_____ static_assert(sizeof(str)==8); #else static_assert(sizeof(str)==4); #endif free(str); // Character array char arr[1024]; static_assert(sizeof(arr)==1024); return 0; } fgets(char *str, int num, FILE *stream) reads until (num-1) characters have been read Instead of fgets(str,sizeof(str),stdin) please fgets(str,n+1,stdin) Fixed version: #include <assert.h> #include <stdio.h> #include <stdlib.h> int main(void){ int n=0; printf("Length? "); scanf("%d",&n); getchar(); char *str=(char*)calloc((n+1),sizeof(char)); static_assert( sizeof(str)==sizeof(char*) && ( sizeof(str)==4 || // 32-bit machine sizeof(str)==8 // 64-bit machine ) ); fgets(str,n+1,stdin); for(int i=0;i<n;++i) printf("%c\n",str[i]); free(str); str=NULL; } Length? 5 abcde a b c d e
Control FlashAir SD card from Linux User Space
I want to control FlashAir SD card from Linux User Space. I decide to choose mmc ioctl to send command to SD card. I have refer to here and here My code is #define MMC_BLOCK_MAJOR 179 #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <linux/mmc/ioctl.h> #include <linux/ioctl.h> #include <linux/types.h> #include <sys/ioctl.h> #define MMC_RSP_PRESENT (1 << 0) #define MMC_RSP_136 (1 << 1) /* 136 bit response */ #define MMC_RSP_CRC (1 << 2) /* expect valid crc */ #define MMC_RSP_BUSY (1 << 3) /* card may send busy */ #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */ #define MMC_RSP_SPI_S1 (1 << 7) /* one status byte */ #define MMC_RSP_R1 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE) #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC) #define MMC_RSP_R3 (MMC_RSP_PRESENT) #define MMC_SEND_STATUS 13 #define MMC_READ_SINGLE_BLOCK 17 #define READ_MULTIPLE_BLOCK 18 #define MMC_READ_EXTR_SINGLE 48 #define MMC_DATA_READ (1 << 9) #define MMC_CMD_AC (0 << 5) #define MMC_CMD_ADTC (1 << 5) #define MMC_CMD_BC (2 << 5) #define MMC_CMD_BCR (3 << 5) uint8_t data_buffer[512]; int main(int argc, char *argv[]) { int i, fd; int ret = 0; struct mmc_ioc_cmd cmd; uint8_t mio = 1, func = 1; uint16_t count = 0x200; uint32_t addr = 0x400; uint32_t offset = addr & 0x1FF; if (offset + count > 512) count = 512 - offset; uint32_t arg = (((uint32_t)mio & 0x1) << 31) | (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | ((addr & 0x1FFFF) << 9) | ((count - 1) & 0x1FF); fd = open("/dev/mmcblk0", O_WRONLY); if(fd < 0) { perror("file open"); return fd; } #if 1 // CMD48 memset(&cmd, 0, sizeof(struct mmc_ioc_cmd)); memset(data_buffer, 0, sizeof(data_buffer)); cmd.opcode = MMC_READ_EXTR_SINGLE; cmd.arg = 0x900001ff; cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; cmd.blksz = 512; cmd.blocks = 1; mmc_ioc_cmd_set_data(cmd, &data_buffer); // I also try with mmc_ioc_cmd_set_data(cmd, data_buffer) ret = ioctl(fd, MMC_IOC_CMD, &cmd); printf("MMC_READ_EXTR_SINGLE response[0] = %x\n", cmd.response[0]); printf("ret = %d\n", ret); printf("data_buffer[1] = %d\n", data_buffer[1]); printf("data_buffer[2] = %d\n", data_buffer[2]); #endif #if 1 // CMD17 memset(&cmd, 0, sizeof(struct mmc_ioc_cmd)); memset(data_buffer, 0, sizeof(data_buffer)); cmd.opcode = MMC_READ_SINGLE_BLOCK; cmd.arg = 0x0; cmd.flags = 0xb5; cmd.blksz = 512; cmd.blocks = 1; mmc_ioc_cmd_set_data(cmd, &data_buffer); // I also try with mmc_ioc_cmd_set_data(cmd, data_buffer) ret = ioctl(fd, MMC_IOC_CMD, &cmd); printf("READ_MULTIPLE_BLOCK response[0] = %x\n", cmd.response[0]); printf("ret = %d\n", ret); printf("data_buffer[1] = %d\n", data_buffer[1]); printf("data_buffer[2] = %d\n", data_buffer[2]); #endif close(fd); return 1; } The ret = 0 ( It seems that there is no error) but the data_buffer[i] always get the value 0. Actually, I want to create an example like Arduino example at this but I use a custom board with Linux. Thanks for any help!
using the FMU in fmu_sdk
I have a small code in C that I want to use to call the IMF functions of fmu_sdk in order to be able to export my model in FMU. If you could tell me how the functions I need to use, here is my program: best regards Mary
Do you want to create a model exchange or co-simulation FMU? Here is a link to a model-exchange FMU that implements an AND for two boolean input values (FMI 2.0, win64): https://www.dropbox.com/s/su8pyjdtg4hs7v1/fmu_boolRef.fmu?dl=0 And here a link to a co-simulation FMU: https://www.dropbox.com/s/bcbl8tf6xb4jc8x/fmu_boolRef.fmu?dl=0 You can compile the the contained source code also to a co-simulation FMU.
#include <stdio.h> #include <stdlib.h> #define vrai 1 #define faux 0 typedef enum BOOLEAN {false, true} bool; bool function_ET(bool e1,bool e2); int main(){ bool e1; bool e2; bool s; printf("entrez les valeur de e1 et e2:"); scanf("%d%d",&e1 ,&e2); s = function_ET(e1,e2); printf("s = %d",s); } bool function_ET(bool e1,bool e2){ return(e1 & e2); }
I was able to write the code C, but I did not manage to have the good result, I can not increment the values of e1 and e2, the values do not change with time, if you could m Help you write the exact code #define MODEL_IDENTIFIER prog_entree1 #define MODEL_GUID "{8c4e810f-3da3-4a00-8276-176fa3c9f013}" // define model size #define NUMBER_OF_REALS 0 #define NUMBER_OF_INTEGERS 0 #define NUMBER_OF_BOOLEANS 3 #define NUMBER_OF_STRINGS 0 #define NUMBER_OF_STATES 0 #define NUMBER_OF_EVENT_INDICATORS 0 // include fmu header files, typedefs and macros #include "fmuTemplate.h" //#include "prog1entrée.c" #define e1 0 #define e2 1 #define s_ 2 void setStartValues(ModelInstance *comp) { b(e1) = 1; b(e2) = 0; } void calculateValues(ModelInstance *comp) { if (comp->state == modelInitializationMode) { b(s_)= b(e1) && b(e2); } } fmi2Boolean getBoolean(ModelInstance* comp, fmi2ValueReference vr) { switch(vr) { case e1 : return b(e1); case e2 : return b(e2); case s_ : return b(s_); } } void eventUpdate(ModelInstance *comp, fmi2EventInfo *eventInfo, int timeEvent, int isNewEventIteration) { } // include code that implements the FMI based on the above definitions #include "fmuTemplate.c" And the result I got after the simulation enter image description here
c code make successfully in Mac while failed in Linux
I have some source files(e.g, layout.cpp). I can use 'make' command in my local MAC machine. After make successfully, I copied all files to remote Linux machine. However, the executable file in my MAC machine could not be executed in remote Linux machine. The error message is below. layout is not a binary executable file I think the failure is due to format of layout file is 'Mach-O 64-bit executable', which couldn't run in linux machine. Therefore, I tried to make source files in remote linux machine. However, it showed a lot of error messages like below. layout.cpp:75: error: ‘exit’ was not declared in this scope layout.cpp:88: error: ‘strcpy’ was not declared in this scope But these errors didn't show in make process in MAC. Are these errors caused by difference of compilers in MAC and Linux? Since there are so many different errors, hence we could not simply add '#include cstdlib' or '#include string.h' to solve that. Thanks. Source Code: #include <iostream> #include <fstream> #include <map> #include <set> #include <string> #include <deque> #include <vector> using namespace std; // layout routines and constants #include <layout.h> #include <parse.h> #include <graph.h> // MPI #ifdef MUSE_MPI #include <mpi.h> #endif int main(int argc, char **argv) { // initialize MPI int myid, num_procs; #ifdef MUSE_MPI MPI_Init ( &argc, &argv ); MPI_Comm_size ( MPI_COMM_WORLD, &num_procs ); MPI_Comm_rank ( MPI_COMM_WORLD, &myid ); #else myid = 0; num_procs = 1; #endif // parameters that must be broadcast to all processors int rand_seed; float edge_cut; char int_file[MAX_FILE_NAME]; char coord_file[MAX_FILE_NAME]; char real_file[MAX_FILE_NAME]; char parms_file[MAX_FILE_NAME]; int int_out = 0; int edges_out = 0; int parms_in = 0; float real_in = -1.0; // user interaction is handled by processor 0 if ( myid == 0 ) { if ( num_procs > MAX_PROCS ) { cout << "Error: Maximum number of processors is " << MAX_PROCS << "." << endl; cout << "Adjust compile time parameter." << endl; #ifdef MUSE_MPI MPI_Abort ( MPI_COMM_WORLD, 1 ); #else exit (1); #endif } // get user input parse command_line ( argc, argv ); rand_seed = command_line.rand_seed; edge_cut = command_line.edge_cut; int_out = command_line.int_out; edges_out = command_line.edges_out; parms_in = command_line.parms_in; real_in = command_line.real_in; strcpy ( coord_file, command_line.coord_file.c_str() ); strcpy ( int_file, command_line.sim_file.c_str() ); strcpy ( real_file, command_line.real_file.c_str() ); strcpy ( parms_file, command_line.parms_file.c_str() ); } // now we initialize all processors by reading .int file #ifdef MUSE_MPI MPI_Bcast ( &int_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD ); #endif graph neighbors ( myid, num_procs, int_file ); // finally we output file and quit float tot_energy; tot_energy = neighbors.get_tot_energy (); if ( myid == 0 ) { neighbors.write_coord ( coord_file ); cout << "Total Energy: " << tot_energy << "." << endl << "Program terminated successfully." << endl; } // MPI finalize #ifdef MUSE_MPI MPI_Finalize (); #endif }
You are missing several standard #include notably the standard C++ header <cstdlib> or the C header <stdlib.h> (for exit) and the standard C++ header <cstring> or the C header <string.h> (for strcpy), as commented by Jonathan Leffler (who also explained why that works on MacOSX but not on Linux). You probably should switch to C++11 standard. This means installing a recent GCC (4.8) and compile with g++ -std=c++11 and of course -Wall -g (to get all warnings and debugging information) .... And you did not search enough on these issues. You could have typed man exit to get exit(3) man page, or man strcpy to get strcpy(3) man page. Both man pages give relevant C include headers.... BTW, the bug is really in your source code. A code using exit really should include <stdlib.h> or <cstdlib> explicitly by itself (at least for readability reasons). You should not suppose that some other system header is (accidentally) including that.