Building with libjson on OSX Mavericks - xcode

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.

Related

The compiler I used is gcc5.1 but it seems that he did't support c++11

#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.

clang pass variables by reference

I am compiling the following code using clang 3.4.2 ..
#include <stdio.h>
void haa(int& j){
j=1;
}
int main(){
printf("hello\n");
}
This gives the following error:
hello.c:3:13: error: expected ')'
void haa(int& j){
^
hello.c:3:9: note: to match this '('
void haa(int& j){
^
hello.c:3:13: error: parameter name omitted
void haa(int& j){
^
hello.c:4:2: error: use of undeclared identifier 'j'
j=1;
^
3 errors generated.
Compiling the same with gcc gives no errors or warnings...
Can someone explain why this is happening?
The issue is that pass by reference (with references and not pointers) is not a c but a c++ feature.
You need to compile the code with a c++ compiler such as g++ or clang++. Changing the file extension to .cpp also works, as this tells the compiler to treat it as a c++.

Windows 7 MinGW compilation error using Boost ASIO

Having trouble compiling the following C++ code on Windows 7:
#include <boost/asio.hpp>
#include <iostream>
void handler1(const boost::system::error_code &ec)
{
std::cout << "5 s." << std::endl;
}
void handler2(const boost::system::error_code &ec)
{
std::cout << "10 s." << std::endl;
}
int main()
{
boost::asio::io_service io_service;
boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5));
timer1.async_wait(handler1);
boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(10));
timer2.async_wait(handler2);
io_service.run();
}
I have MinGW installed (gcc 4.8.1) in c:\mingw with my PATH set up correctly. I have downloaded boost and declared environment variable BOOST_ROOT to be the path where it resides. I have gone through the bootstrap and b2 procedure for boost. I now try and compile:
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -o main main.cpp
Gives a bunch of error: '::UnregisterWaitEx' has not been declared errors
I then search a bit and see I may need to link boost_system. So:
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -lboost_system -o main main.cpp
Same errors. Thought I'd try specify library path. Did a search for boost_system and found static libs (libboost_system-mgw48-mt-1_55.a) in %BOOST_ROOT%/stage/lib. So
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp
Same errors. So I search again and see others suggesting appending a -D-D_WIN32_WINNT=0x0601. So
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp -D_WIN32_WINNT=0x0601
And the inevitable errors:
c:\mingw\include\mswsock.h:125:20: error: 'WSAPOLLFD' was not declared in this scope
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:36: error: expected primary-expression before ',' token
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:41: error: expected primary-expression before ')' token
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:41: error: expression list treated as compound expression in initializer [-fpermissive]
Where am I going wrong?
I went ahead and rebuilt Boost again with b2 toolset=gcc --build-type=complete. Same thing happened. Finally, after all that, it turned out all I needed was to put the linking at the end of the command:
C:\path\to\sandbox> g++ -D_WIN32_WINNT=0x0601 -I%BOOST_ROOT% -L%BOOST_ROOT%\stage\lib -o boosttest boosttest.cpp -lwsock32 -lws2_32 -lboost_system-mgw48-mt-d-1_55
C:\path\to\sandbox> boosttest.exe
5 s.
10 s.
The -D_WIN32_WINNT was still necessary and, for anyone who has skipped the other comments, I had to patch winsock.h as detailed http://sourceforge.net/p/mingw/bugs/1980/. And remember to put %BOOST_ROOT%\stage\lib in your PATH so Windows can find the dll at runtime.
Arduous

C code compilation problems in VS2010

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.

Pthreads compile not working

I have written some code but it doesn't seem to work when I compile it. I am trying to run this in Ubuntu:
#include <pthread.h>
#include <ctype.h>
#include <unistd.h>
char buffer[128];
void *write_thread(void *args)
{
int count = *((int*)args);
write(STDOUT_FILENO, buffer, count);
pthread_exit(0);
}
void *toupper_thread(void *args)
{
int i;
int count = *((int*)args);
for(i = 0; i < count; i++) {
buffer[i] = toupper(buffer[i]);
}
pthread_t writeId;
pthread_create(&writeId, NULL, write_thread, &count);
pthread_join(writeId, NULL);
pthread_exit(0);
}
void *read_thread(void *args)
{
int count = read(STDIN_FILENO, buffer, 128);
pthread_t toupperId;
pthread_create(&toupperId, NULL, toupper_thread, &count);
pthread_join(toupperId, NULL);
//buffer[count] = 0;
pthread_exit(0);
}
int main()
{
pthread_t threadId;
pthread_create(&threadId, NULL, read_thread, NULL);
pthread_join(threadId, NULL);
}
and I get these errors when I try to compile it with gcc -pthread prob41.c or with gcc prob41.c -lpthread:
prob41.c:1:21: error: pthread.h: No such file or directory
prob41.c: In function ‘toupper_thread’:
prob41.c:23: error: ‘pthread_t’ undeclared (first use in this function)
prob41.c:23: error: (Each undeclared identifier is reported only once
prob41.c:23: error: for each function it appears in.)
prob41.c:23: error: expected ‘;’ before ‘writeId’
prob41.c:24: error: ‘writeId’ undeclared (first use in this function)
prob41.c: In function ‘read_thread’:
prob41.c:33: error: ‘pthread_t’ undeclared (first use in this function)
prob41.c:33: error: expected ‘;’ before ‘toupperId’
prob41.c:34: error: ‘toupperId’ undeclared (first use in this function)
prob41.c: In function ‘main’:
prob41.c:45: error: ‘pthread_t’ undeclared (first use in this function)
prob41.c:45: error: expected ‘;’ before ‘threadId’
prob41.c:46: error: ‘threadId’ undeclared (first use in this function)
I don't know what I'm doing wrong.
It seems that you missed some dependencies.
Try this:
$ sudo apt-get install -y libc6-dev
Need to add -I flag in the compile command to enable the compiler to find pthread.h
You don't have a pthread.h library in the system. Try to install the
suitable library for it.
You need to be sure to use #include <sys/type.h>
Don't forget to add the -lpthread while compiling: gcc -lpthread {x}.c -o {y}

Resources