B
bugbuster_bk
Guest
I am trying to compile a simple regular expression C program with visual studio 2017 developer command prompt. My developer command prompt opens up as follows
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.7
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community>
When I tried to compile the program it told me that "regex.h" was not found. Some google searching suggested I use
#include <regex>
When I tried to compile using that header the compile aborted due to maximum limit of 100 errors exceeded.
When I tried to use a version of "regex.h" I had lying around on my laptop, the code compiled but when the executable was not build due to missing definitions of several functions
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
regex1.c
Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:regex1.exe
regex1.obj
regex1.obj : error LNK2019: unresolved external symbol _regcomp referenced in function _main
regex1.obj : error LNK2019: unresolved external symbol _regexec referenced in function _main
regex1.obj : error LNK2019: unresolved external symbol _regerror referenced in function _main
regex1.exe : fatal error LNK1120: 3 unresolved externals
My source code (using my version of regex.h) is as follows
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdarg.h>
6 #include "regex.h"
7
8 int main(int argc, char *argv[])
9 {
10 int length , argnum;
11 char *argptr , pattern[1024] , prematch[1024] , matched[1024];
12 char postmatch[1024];
13 int errcode;
14 char errmsg[256];
15 regex_t expression;
16 regmatch_t pmatch[2];
17
18 if ( argc < 3 ) {
19 fprintf(stderr,"Usage : %s pattern string1 [... string_n]\n",argv[0]);
20 exit(1);
21 } /* IF */
22
23 strcpy(pattern,argv[1]);
24 errcode = regcomp(&expression, pattern, REG_ICASE | REG_EXTENDED);
25 if ( errcode != 0 ) {
26 regerror(errcode,&expression,errmsg,sizeof(errmsg));
27 fprintf(stderr,"Bad data pattern : %s\n",errmsg);
28 exit(1);
29 } /* IF */
30
31 argptr = argv[2];
32 for ( argnum = 2 ; argnum < argc ; argptr = argv[++argnum] ) {
33 errcode = regexec(&expression, argptr, (size_t)1, pmatch, 0);
34 if ( errcode == 0 ) {
35 strncpy(prematch, argptr, pmatch[0].rm_so);
36 prematch[pmatch[0].rm_so] = '\0';
37 length = pmatch[0].rm_eo - pmatch[0].rm_so;
38 strncpy(matched,&argptr[pmatch[0].rm_so],length);
39 matched[length] = '\0';
40 strcpy(postmatch, &argptr[length + strlen(prematch)]);
41 printf("%s -- Yes at offset %d , length = %d [%s] , pre = [%s]",
42 argptr, pmatch[0].rm_so,length,matched,prematch);
43 printf(" , post = [%s]\n",postmatch);
44 } /* IF */
45 else {
46 printf("%s -- No\n",argptr);
47 } /* ELSE */
48 } /* FOR */
49
50 exit(0);
51 } /* end of main */
How can I get my code to compile and link properly ? Is there something I need to download ? I had recently downloaded/installed visual studio 2017 so I thought i would have everything up to date.
Continue reading...
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.7
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community>
When I tried to compile the program it told me that "regex.h" was not found. Some google searching suggested I use
#include <regex>
When I tried to compile using that header the compile aborted due to maximum limit of 100 errors exceeded.
When I tried to use a version of "regex.h" I had lying around on my laptop, the code compiled but when the executable was not build due to missing definitions of several functions
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
regex1.c
Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:regex1.exe
regex1.obj
regex1.obj : error LNK2019: unresolved external symbol _regcomp referenced in function _main
regex1.obj : error LNK2019: unresolved external symbol _regexec referenced in function _main
regex1.obj : error LNK2019: unresolved external symbol _regerror referenced in function _main
regex1.exe : fatal error LNK1120: 3 unresolved externals
My source code (using my version of regex.h) is as follows
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdarg.h>
6 #include "regex.h"
7
8 int main(int argc, char *argv[])
9 {
10 int length , argnum;
11 char *argptr , pattern[1024] , prematch[1024] , matched[1024];
12 char postmatch[1024];
13 int errcode;
14 char errmsg[256];
15 regex_t expression;
16 regmatch_t pmatch[2];
17
18 if ( argc < 3 ) {
19 fprintf(stderr,"Usage : %s pattern string1 [... string_n]\n",argv[0]);
20 exit(1);
21 } /* IF */
22
23 strcpy(pattern,argv[1]);
24 errcode = regcomp(&expression, pattern, REG_ICASE | REG_EXTENDED);
25 if ( errcode != 0 ) {
26 regerror(errcode,&expression,errmsg,sizeof(errmsg));
27 fprintf(stderr,"Bad data pattern : %s\n",errmsg);
28 exit(1);
29 } /* IF */
30
31 argptr = argv[2];
32 for ( argnum = 2 ; argnum < argc ; argptr = argv[++argnum] ) {
33 errcode = regexec(&expression, argptr, (size_t)1, pmatch, 0);
34 if ( errcode == 0 ) {
35 strncpy(prematch, argptr, pmatch[0].rm_so);
36 prematch[pmatch[0].rm_so] = '\0';
37 length = pmatch[0].rm_eo - pmatch[0].rm_so;
38 strncpy(matched,&argptr[pmatch[0].rm_so],length);
39 matched[length] = '\0';
40 strcpy(postmatch, &argptr[length + strlen(prematch)]);
41 printf("%s -- Yes at offset %d , length = %d [%s] , pre = [%s]",
42 argptr, pmatch[0].rm_so,length,matched,prematch);
43 printf(" , post = [%s]\n",postmatch);
44 } /* IF */
45 else {
46 printf("%s -- No\n",argptr);
47 } /* ELSE */
48 } /* FOR */
49
50 exit(0);
51 } /* end of main */
How can I get my code to compile and link properly ? Is there something I need to download ? I had recently downloaded/installed visual studio 2017 so I thought i would have everything up to date.
Continue reading...