I'm trying to compile C programs in VS2010 Professional using cl.exe (64bit command line). Getting strange errors in VS2010 or VS2008. Same Code compiles & runs in GNU gcc without a problem (Cygwin). Any ideas? Can't go any further with the real thing until I understand the problem here. Thanks!
filename: testC.c
cl.exe testC.c
#include<stdio.h>
#include <stdlib.h>
typedef double Td;
int main(int argc, char *argv[])
{
FILE *fp;
if ( (fp=fopen("junk_out.txt","w")) == NULL ){
printf("Cannot open file.\n");
exit(1);
}
fprintf(fp,"%f \n",3.1420);
fclose(fp);
Td x=3.14;
Td *a;
a = &x;
printf("%f \n",a);
printf("%f \n",x);
printf("%f \n",*a);
return 0;
}
Here is the output:
testC.c(18): error C2275: 'Td' : illegal use of this type as an expression
testC.c(5) : see declaration of 'Td'
testC.c(18): error C2146: syntax error : missing ';' before identifier 'x'
testC.c(18): error C2065: 'x' : undeclared identifier
testC.c(18): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
testC.c(19): error C2275: 'Td' : illegal use of this type as an expression
testC.c(5) : see declaration of 'Td'
testC.c(19): error C2065: 'a' : undeclared identifier
testC.c(21): error C2065: 'a' : undeclared identifier
testC.c(21): error C2065: 'x' : undeclared identifier
testC.c(21): warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
testC.c(22): error C2065: 'a' : undeclared identifier
testC.c(23): error C2065: 'x' : undeclared identifier
testC.c(24): error C2065: 'a' : undeclared identifier
testC.c(24): error C2100: illegal indirection
You have to define every variable at the top of a function if you compile your code with the C compiler from VS2010.
#include <stdio.h>
#include <stdlib.h>
typedef double Td;
int main(int argc, char *argv[])
{
FILE *fp;
Td x;
Td *a;
if ( (fp=fopen("junk_out.txt","w")) == NULL )
{
printf("Cannot open file.\n");
exit(1);
}
fprintf(fp,"%f \n",3.1420);
fclose(fp);
x=3.14;
a = &x;
printf("%f \n",a);
printf("%f \n",x);
printf("%f \n",*a);
return 0;
}
In C++ you can define everywhere you want.
Related
I tried to compile OpenVDB on Windows11(VS2017), but confusing error occurs:
Error C2676 Binary '<': 'const _Ty' does not define the operator or a conversion to a type that can be received by a predefined operator openvdb_shared
Error C2056 invalid expressions
Error C2088 invalid for class
It points to <algorithm.h> file, line 5386: "_NOEXCEPT_COND(_NOEXCEPT_OPER(_Left < _Right))".
#pragma warning(push)
#pragma warning(disable: 28285) // (syntax error in SAL annotation, occurs when _Ty is not an integral type)
template<class _Ty>
_Post_equal_to_(_Left < _Right ? _Right : _Left)
_NODISCARD constexpr const _Ty& (max)(const _Ty& _Left, const _Ty& _Right)
_NOEXCEPT_COND(_NOEXCEPT_OPER(_Left < _Right))
{ // return larger of _Left and _Right
if (_Left < _Right)
{
_STL_ASSERT(!(_Right < _Left), "invalid comparator");
return (_Right);
}
return (_Left);
}
and anothor fatal error:
fatal error LNK1104: can not open file “....\openvdb\openvdb\Debug\openvdb.lib”
I download OpenVDB source code and dependencies by git and vcpkg following the manual OpenVDB. I have no idea about how to fix those problems. Any suggestions or solution would be greatly appreciated!!!
I have a simple program to test basename() method:
#include <stdio.h>
#include <string.h>
//#include <libgen.h>
int main (int argc , char **argv)
{
char *a = strdup("test();");
printf("%s", basename(a));
return 0;
}
clang complains but it compiles anyway.
test.c:7:18: warning: implicit declaration of function 'basename' is invalid in C99 [-Wimplicit-function-declaration]
printf("%p", basename(a));
^
test.c:7:18: warning: format specifies type 'void *' but the argument has type 'int' [-Wformat]
printf("%p", basename(a));
And it results in segfault. But if I added libgen.h header, it works normally.
I've checked the binary with otool, it linked against the same dylib, nothing else. Why does the first one crash?
I've checked the answer here already, it uses a static buffer but I use the result from strdup(), so it's a different question.
#include <iostream>
#include <string>
int main()
{
std::string s1 = "hello";
std::cout << s1 << std::endl;
for (auto c : s1)
std::cout << c << std::endl;
return 0;
}
I think the code is right, but it had lots of error message(please ignore the name of the code):
D:\>gcc hello.cpp -o hello
hello.cpp: In function 'int main()':
hello.cpp:8:12: error: 'c' does not name a type
for (auto c : s1)
^
hello.cpp:11:2: error: expected ';' before 'return'
return 0;
^
hello.cpp:11:2: error: expected primary-expression before 'return'
hello.cpp:11:2: error: expected ';' before 'return'
hello.cpp:11:2: error: expected primary-expression before 'return'
hello.cpp:11:2: error: expected ')' before 'return'
The book that i used to learn c++ is c++ primer, and I have learned c language before, but i still am a freshmen.
you need to that the flag -std=c++11 (C++11) or -std=c++1y(C++14) to your compile command.
I've tried the advice on threads here and here to no avail.
I have Xcode 5.0.2 installed and I am compiling everything on the command line. After make/make install to build libjson, I created a simple test file to link and build from it:
#include <iostream>
#include "libjson.h"
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
JSONNode n(JSON_NODE);
n.push_back(JSONNode("RootA", "Hello World"));
JSONNode c(JSON_ARRAY);
c.set_name("ArrayOfNumbers");
c.push_back(JSONNode("", 16));
c.push_back(JSONNode("", 42));
c.push_back(JSONNode("", 128));
n.push_back(c);
std::string jc = n.write_formatted();
std::cout << jc << std::endl;
return 0;
}
When I try to build this file:
g++ -DNDEBUG main.cpp -ljson
I get this:
main.cpp:17:5: error: unknown type name 'JSONNode'
JSONNode n(JSON_NODE);
^
main.cpp:18:17: error: use of undeclared identifier 'JSONNode'
n.push_back(JSONNode("RootA", "Hello World"));
^
main.cpp:19:5: error: unknown type name 'JSONNode'
JSONNode c(JSON_ARRAY);
^
main.cpp:21:17: error: use of undeclared identifier 'JSONNode'
c.push_back(JSONNode("", 16));
^
main.cpp:22:17: error: use of undeclared identifier 'JSONNode'
c.push_back(JSONNode("", 42));
^
main.cpp:23:17: error: use of undeclared identifier 'JSONNode'
c.push_back(JSONNode("", 128));
Found the answer from another SO question after I realized the make process had problems. Basically, the solution is to copy the source code into Xcode and build it as part of the project instead of trying to link it as a library.
I also tried to build libjson 7.6.1 on an ubuntu machine (12.04) and encountered the exact problem despite a perfect make.
I am working on a project where I have to support IPv6 addressing scheme. This requires me to replace winsock.h with winsock2.h to make IP-version independent calls. Initially, I faced redefinition related errors which I could remove by employing solutions available online (placing #include <winsock2.h> before #include <windows.h>; including #define_WINSOCKAPI_). However, I am still getting errors like:
2>../include\obsocket.h(171) : error C2143: syntax error : missing '}' before 'constant'
2>../include\obsocket.h(171) : error C2059: syntax error : 'constant'
2>../include\obsocket.h(171) : error C2143: syntax error : missing ';' before '}'
2>../include\obsocket.h(171) : error C2238: unexpected token(s) preceding ';'
2>../include\obsocket.h(173) : error C2065: 'ShutdownType' : undeclared identifier
2>../include\obsocket.h(177) : error C2146: syntax error : missing ';' before identifier 'GetDescriptor'
2>../include\obsocket.h(177) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(177) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(177) : error C2065: '_sock' : undeclared identifier
2>../include\obsocket.h(179) : error C2065: '_timeout' : undeclared identifier
2>../include\obsocket.h(180) : error C2065: '_use_blocking_calls' : undeclared identifier
2>../include\obsocket.h(181) : error C2065: '_req_pending' : undeclared identifier
2>../include\obsocket.h(185) : error C2270: 'ToString' : modifiers not allowed on nonmember functions
2>../include\obsocket.h(187) : error C2059: syntax error : 'private'
2>../include\obsocket.h(189) : error C2146: syntax error : missing ';' before identifier '_sock'
2>../include\obsocket.h(189) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(189) : error C2086: 'int SOCK_TYPE' : redefinition
2>../include\obsocket.h(177) : see declaration of 'SOCK_TYPE'
2>../include\obsocket.h(189) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(189) : error C2371: '_sock' : redefinition; different basic types
2>../include\obsocket.h(199) : error C2371: '_use_blocking_calls' : redefinition; different basic types
2>../include\obsocket.h(200) : error C2371: '_timeout' : redefinition; different basic types
2>../include\obsocket.h(201) : error C2371: '_req_pending' : redefinition; different basic types
2>../include\obsocket.h(203) : error C2146: syntax error : missing ')' before identifier 'sock'
2>../include\obsocket.h(203) : error C2146: syntax error : missing ';' before identifier 'sock'
2>../include\obsocket.h(203) : error C2371: 'SOCK_TYPE' : redefinition; different basic types
I've made sure that winsock.h is not getting included anywhere during the build (by enabling show includes during the build).
Here is the code for obsocket.h:
#ifndef _obsocket_h
#define _obsocket_h
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
//#include <winsock.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
class ObSocket {
public:
#ifdef _WIN32
typedef SOCKET SOCK_TYPE;
#else
typedef int SOCK_TYPE;
#endif
ObSocket();
~ObSocket();
enum ShutdownType { SD_INCOMING = 0, SD_OUTGOING = 1, SD_BOTH = 2 };
ObStatus Shutdown(ShutdownType);
ObStatus Close();
bool IsValid();
SOCK_TYPE GetDescriptor() { return _sock; }
void NonBlocking(int timeout);
inline int GetTimeout( ) { return _timeout; }
inline bool IsBlocking( ) { return _use_blocking_calls; }
inline int IncPendingReq(int i = 1) { _req_pending += i; return _req_pending; }
inline int DecPendingReq(int i = 1) { _req_pending -= i; return _req_pending; }
inline int GetPendingReq( ) { return _req_pending; }
ObString& ToString(ObString& strClass) const;
private:
SOCK_TYPE _sock;
ObInetAddress _addr;
char _my_addr[20];
in_port_t _my_port;
char _remote_addr[20];
in_port_t _remote_port;
bool _use_blocking_calls;
int _timeout;
int _req_pending;
ObSocket(SOCK_TYPE sock);
void SetAddr(sockaddr_in& inAddr, const ObInetAddress& addr, in_port_t port);
void ReallocFileDescriptor();
ObSocket(const ObSocket&);
ObSocket& operator=(const ObSocket&);
static void Initialize();
static void Finalize();
};
Can anyone help in pointing out what could be going wrong?