A couple of Eclipse C/C++ questions - c

I've been using VS2008/2010 for a while and I'm going to learn using Eclipse Helions for C/C++ development (mainly C).
I am abit confused about libraries and includes though.
1) How do you properly include winsock2.h for example? I've tried this:
#ifndef CONFIG_H_
#define CONFIG_H_
/* Windows-Build */
#if defined(WIN32) || defined(_WIN32)
#include <winsock2.h>
#endif
SOCKET sock;
#endif /* CONFIG_H_ */
But the compiler dosn't recognise SOCKET. Do you have to manually add the full path to winsock2 somewhere?
2) What about ws2_32.lib? Where do you include that in Eclipse? Do you have to add a path as well?
3) Having used VS mostly I'm new to makefiles. How do you include custom makefiles? Is there a good guide for starting with makefiles?
4) Is there a intellisense like in VS?
That's what I can figure out right now.
Thanks!
EDIT:
In response to the first answer:
Building target: Filesharing_core.dll
Invoking: Cygwin C Linker
gcc -L"C:\cygwin\lib\w32api" -shared -o"Filesharing_core.dll" ./src/test.o -llibws2_32.a
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -llibws2_32.a
collect2: ld returned 1 exit status
make: *** [Filesharing_core.dll] Error 1

First off, cygwin will usually use GCC as it's compiler. That means that there's no special windows support that you'll find in the VS compiler and editor. Be prepared to get your hands dirty.
Keep in mind that my CDT version is a bit old and I don't have Cygwin installed so some of the things below might not be accurate. Also all the compiler options that I mention are detailled in the gcc manual.
1) In your sample, nobody is defining WIN32 or _WIN32 (I don't think GCC is going to do that for you but do check). To fix that, you'll have to include the windows.h header which (I think) will take care of definning the correct macros. You could also use the -D compiler switch (configurable in your makefile or through the eclipse menus).
If you encounter missing include errors once you've fixed the defines, I believe that the windows headers are located in the C:\cygwin\usr\include\w32api folder. To add that to your include path, simply open your project properties and navigate to C/C++ General > Paths and Symbols. Add the path to the GNU C group in the Includes tabs. Depending on how you configured your project, this might have already been done for you.
If you're building with your own makefile, you should still do the previous step because it will allow the indexer to find and parse those headers. To tell the compiler about the include folder, use the -I switch.
2) I'm going to guess that ws2_32.lib is the lib file for winsock2. If this is the case, I'm not entirely sure who's responsible for building it (is there a .dll you can use instead?). You might want to check your c:\cygwin\usr\lib folder or the c:\cygwin\usr\local\lib.
If you're using a managed project (eclipse builds the makefile for you) then go to your project properties and navigate to C/C++ Build > Settings. In the Tool Settings tab, go to the libraries item in the linker section. Just add the name of the lib file and the folder in the appropriate boxes.
In your own makefile you'll want to use the -l compiler switch to specify a library and the -L compiler switch to specify a search path.
3) A good place to get started with makefiles would be the GNU make manual.
One detail about running a Makefile on Windows: make sure to use the shell provided by Cygwin or MinGW. Otherwise, commands like rm won't be defined and it'll make your life very difficult.
You might also want to consider CMake. It's easier to use and scales better to larger projects.
To use a hand-made makefile in your project, just create an new Makefile project and dump your Makefile file in the root of your project folder. That's it.
4) Yes there is but it's not called intellisense. Just hit CTRL+Space anywhere in your source code to bring it up.
Other fun tools can be found in the right-click menu. My personal favorites include CTRL+SHIFT+R to find and open a file, CTRL+SHIFT+T to find and open a type\variable\function\define and CTRL+O to find and goto a type\variable\function\define within the opened file.
The indexer can go a little crazy sometimes (mostly when parsing C++ code). You can modify its behaviour by going in the Windows > Preferences menu at the top and navigating to the C/C++ > Indexer item.
I hope this helps.

Related

gtk.h missing in Visual Studio for Linux Development

I'm currently trying to write an app for Raspberry Pi 3B under Rasbpian with aid of Linux Development plugin in Visual Studio 2017 Community. I managed to successfully deploy 'Blink' example, nobly attached by Microsoft folks, according to tutorial, and that went well. I even made some transmission over SPI thanks to wiringPi library. Then I would like to add some GUI to my app, so that one could, for example, make some transmission on click of a button on screen.
IntelliSense hinted me, that, in fact, there is gtk-3.0 library present in toolset. It seems that libraries are being copied from target device on every connection or so and I installed gtk on my Raspberry. So I added a simple line to this Blink example:
#include <gtk-3.0/gtk/gtk.h>
On compilation attempt, of course there was nearly 4k errors. Well, enough said, with a little hint from this old tutorial and a bit of trial and error, I managed to add this set of links under Debugging/Project properties/Configuration properties/VC++ directories/Header files directories:
Everything goes in promising direction, as errors number diminished from 4k to just one:
gtk-3.0\gtk\gtk.h: No such file or directory
No matter that this file is ACTUALLY in this location:
Regardless of combination of links in configuration above and using statement composition, compiler (?) can't find this damn file.
Please Halp
EDIT
I just confirmed, that it is indeed problem with target configuration. This is bad or good, depending on point of view. Good, because there is probably all good with VS setup. Bad, because I don't know a thing about compiling things under Linux.
On target (Raspberry Pi 3B) all ingredients for compilation are copied by Linux Development plugin. So in Terminal I executed line:
g++ main.cpp -o Blink2onRPi
and got
main.cpp:4:21: fatal error: gtk/gtk.h: no such file or directory
Now, I altered include line in main.cpp on target RPi, to this:
#include <gtk-3.0/gtk/gtk.h>
And now its missing <gdk/gdk.h>! When this change is made on host windows device - same result, but in VS.
As I dealt with similar problem in VS, upon setting links for IntelliSense (now apparently they're for this purpose), now probably similar dependencies have to be set somewhere on Raspbian. But where?
EDIT2
Upon execution of:
g++ main.cpp -o Blink2onRPi `pkg-config --cflags --libs gtk+-3.0`
on target RPi there is no more GTK-related errors, just wiringPi (also present in project) undefined references. It raises two possible questions:
1) How can I setup wiringPi on RPi so that the project could be manually compiled on target and
2) How/where add above line to Visual Studio, so it execute remotely with all GTK dependencies added properly on target
Researching stock present wiringPi library (as this is Blink led example for cross-compile Linux Development) I've found, that in Project Properties/Linker/Input/Library Dependencies there is mysterious entry:
wiringPi
Just that, nothing more. After removing this entry, on compilation pops out same errors as before on target (which apparently lacks proper wiringPi setup) - undefined references (not mensioned any missing headers). Can this be relevant for the case? If so, how could I add there such entry which would deal with missing GTK dependencies?
TL;DR
Use screenshot below to know where to add pkg-config calls in VS configuration so that it forwards it to the compiler and linker on the target.
Thanks to #zaguoba for providing these.
ORIGINAL ANSWER:
The list of directories to include is provided by pkg-config. For example pkg-config --cflags-only-I gtk+-3.0 will give you the list of include directories required. Those are the ones you need to add to the directories where VC++ wil look at include files. If you add the relative path you use in the #include, to one of those paths, you are able to find the file.
Example:
If you add to the directories C:\Program Files\foo\bar\gtk+-3.0
and have in your C file:
#include <gtk/gtk.h>
then the compiler will look for C:\Program Files\foo\bar\gtk+-3.0\gtk\gtk.h.
EDIT:
This all means the 'file not found' errors are because you're really building on the target and the target has no idea what C:\Program Files\... means. Those should be paths on the target filesystem, where the compiler is called. And this is exactly what pkg-config provides.
The copy of those files on the Windows machine filesystems is merely for Intellisense use, not for compiler use.
EDIT 2:
So that's that Visual Studio 2017 Community Linux Development plugin is what need to be undestood. It's not for cross compilation from Windows to Linux, istead it merely synchronizes code to the Windows host (for Intellisense use), but builds on the target. This means that all the paths and commands are Linux paths and commands, run on the target.
Here's the OP working configuration:
With that setup, you should
#include <gtk\gtk.h>
instead of
#include <gtk-3.0\gtk\gtk.h>
Alternatively remove all those VC++ directories/Header files directories, and just keep one of them that ends with include/ instead of listing up all the sub directiores.

Dev-C++: Why isn't curses.h included?

I've installed Orwell Dev-C++ 5.x and the PDCurses DevPak. However, when I try to compile the example curses application, the compiler/linker complains that it can't find curses.h. Why?
Before reading any further, keep in mind that Dev-C++ is old software, the use of which is not recommended. Rather use Code:Blocks, Visual Studio or something similar.
The issue being experienced is as a result of the project's configuration being incomplete. Make sure that the "Includes" section (Project Properties) has an entry for the directory containing curses.h ( {installDir}/include) and "Libraries" (also in "Project Properties") has an entry for {installDir}/libs, which contains curses.a.
Ideally, these directories should be set as part of the IDE configuration rather than at the project level. The process of doing so is similar to that described above.
Note that you will have to add the appropriate directories for the curses library and header to a custom makefile (-I for includes and -L for libraries) as part of the project. (Adding it to the auto-generated makefile will not work.) To do this:
Save a copy of the makefile (Makefile.win) made on first compilation (which failed) as Makefile.curses.
Edit the makefile to contain the appropriate directives (-I{installDir}/include after the other includes and -L{installDir}/libs after the other libraries).
In the project's configuration/build options, check 'Use Custom Makefile' and set it to Makefile.curses.

Compiling C Source with 'make'

I'm trying to compile sqlite3 for Windows 64-bit using their amalgamation source with 'autoconf' from here: http://www.sqlite.org/sqlite-amalgamation-3071201.zip
Someone posted on my previous question Need 64-bit SQLite DLL for managed C# application some preprocessor defines that I need to use:
WIN64 NDEBUG
_WINDOWS
_USRDLL
NO_TCL
_CRT_SECURE_NO_DEPRECATE
THREADSAFE=1
TEMP_STORE=1
SQLITE_MAX_EXPR_DEPTH=0
I hunted around to try and find out how to actually make this work - I'm a newbie to all this. I remembered that Linux had a 'make' tool, so I fired up my Ubuntu VM, installed make and gcc, then prepared the source in a new directory.
From reading other posts and Internet articles, I gathered some hints that preprocessor defines can go in the Makefile, so I did this somewhere after all the other defines in Makefile.in to add the defines to the CFLAGS variable I found:
CFLAGS+=-DWIN64 NDEBUG
CFLAGS+=-D_WINDOWS
CFLAGS+=-D_USRDLL
CFLAGS+=-DNO_TCL
CFLAGS+=-D_CRT_SECURE_NO_DEPRECATE
CFLAGS+=-DTHREADSAFE=1
CFLAGS+=-DTEMP_STORE=1
CFLAGS+=-DSQLITE_MAX_EXPR_DEPTH=0
I did a bash configure then a make, and it happily did some stuff. However, the only binary/object files in the directory now are shell.o and sqlite3.o. Now as far as I know, these need to be linked first to get some workable machine code - so they're not what I'm looking for. I was expecting a DLL file to come out somewhere, or at least something resembling that.
What am I doing wrong?
Update
I originally used CFLAGS:=, which I thought was to append, but turns out it's supposed to be +=.
Now it gives the following error - ar: sqlite3.o: No such file or directory

Strange disconnect between Eclipse CDT, included system headers, and the underlying C build

I'm having a strange problem with include files and an apparent disconnect between Eclipse's build process and its error reporting. I'll give detailed steps to reproducing this problem, so we can narrow the causes down as quickly as possible.
Eclipse 3.7.1 (Indigo SR1) for C/C++ Linux Developers, Ubuntu 10.10 64-bits
It all started when I imported an existing project that "makes" just fine on its own: I thought Eclipse would help considerably in navigating the files and figuring out what does what. However, some of the #included system headers seemed not to be having the correct effect in Eclipse's view. This was very puzzling, and in the course of investigating this, I managed to recreate the problem in a tiny sand box.
Step one: Create a new C project (File: New: C Project) using the Hello World ANSI C Project sample. The parameters are Executable: Hello World ANSI C Project/Linux GCC, the remaining Empty projects set to Linux GCC, and GNU Autotools set to Hello World ANSI C Autotools Project. Call it "hello". Be sure to generate the makefile automatically (Advanced Settings, I believe it is the default).
Step two: Adjust the include path. Using Project: Properties: C/C++ General: Paths and Symbols: Includes: GNU C, set the search path to /usr/local/include, /usr/lib/gcc/x86_64-linux-gnu/4.4.5/include, /usr/include. The second path depends on the exact version of gcc you have installed. This doesn't matter too much, as long as the build path includes at least /usr/include.
Now if you open hello.c, it looks very simple, and Eclipse is quite happy except for return EXIT_SUCCESS;, which cannot resolve EXIT_SUCCESS. Replace EXIT_SUCCESS with a zero (0) and Eclipse gives the all-clear. Select Project: Build Project to generate the executable.
Open a command-line window and drill down to your Eclipse workspace folder's hello/Debug subfolder. Once there, you can run the executable with the line ./hello.
Now the fun begins. Modify hello.c to read in its latter part:
#include <fcntl.h>
int main(void) {
printf("Hello World!\n");
int zz = SPLICE_F_MOVE;
printf("zz (SPLICE_F_MOVE) is '%d'\n", zz);
printf("Bye World!\n");
return 0;
}
You'll get an error on the int zz... line: "Symbol 'SPLICE_F_MOVE' could not be resolved". If you Build the project, you'll get a similar error in the console view: "error: 'SPLICE_F_MOVE' undeclared".
Now if you change the preamble to:
#define _GNU_SOURCE
#include <fcntl.h>
You still get the error on the int zz line (in the editor), but the project will Build correctly! You can confirm this by running the binary in the command-line window you opened earlier. This is really odd since examination of /usr/include/fcntl.h will show that it #includes <bits/fcntl.h>, and that latter header #defines SPLICE_F_MOVE (and a bunch of others) in a block guarded by #ifdef __USE_GNU (__USE_GNU gets #defined if _GNU_SOURCE is #defined). Why on Earth isn't our #define _GNU_SOURCE properly propagated in the workspace perspective?
So this is my problem in a nutshell: why is it the editor (and all of Eclipse CDT) reports an error (apparently by refusing to process correctly an include), but that the underlying build succeeds?
I had similar problems working with GNU and non-GNU compliant toolchains for cross-compiling various microcontrollers. CDT reports lots of errors, but the actual build works perfectly, even when all the header includes are correctly located in the project settings.
The problem is in how CDT operates as distinct from your make system build. CDT is a standalone lexer, parser, and preprocessor and doesn't make use of the toolchain you're using to do the build. This normally would work fine once you set the project settings to match those generated as command-line arguments to your build tools, but many toolchains frequently include hidden implicit defines as part of their compiler/preprocessor itself. The documentation for the implict/hidden defines is usually sketchy and incomplete in my experience, making it nearly impossible to determine which defines are set for a given toolchain (sometimes even dependent on build options) and what they mean. While it may look like you have all the correct defines set to reach the one you're having a problem with, I usually find (especially with GNU) that there are some you're missing that prevent the switch from being processed the way you expected.
When I debug something like this, I first make sure any headers in my project directory are found first if they have names matching those in other directories, then I slowly start moving headers from the standard include folder to my project folder. CDT doesn't do a very good job of indicating which possible versions of a header file it's parsing first, and it frequently pulls system environment settings when you least expect it, but it does look in your project folder for headers first (if you haven't set the system headers path in your project settings to take precedence). By moving the system headers from the location you're looking at to your project directory, you know for sure exactly which header file is being parsed by CDT. Furthermore, if you rebuild your index after doing the move, then open the file in Eclipse, CDT will indicate what switches it believes are enabled or disabled by greying-out or folding the branches not enabled in the header. Following this process has solved this exact problem for me every time, revealing when I had paths to multiple copies of the same header, accidentally duplicated headers, had defines or includes under unexpected switches, etc.
I have the same issue, but didn't even do anything that complicated to produce it--I just made a hello world project of the type you describe and get the same error. Note that, however, in my case, it may be due to a borked install of cygwin--for instance, I had other errors concerning failing to include the .h files, and a complete deletion/reinstall of cygwin removed those errors but the 'Symbol ... Could not be resolved' error remains.
I then decided to make a second such project, and they both report such an error in the 'problems' view, but only one of the projects actually shows the error underlined etc. in the editor (!)
Maybe this issue comes from Ijndigo itself. Did you tried the same with the latest Indigo SR2? I opened a number of bugs for Indigo and its SR1 in eclipse bugzilla.

Set compiler includes (-I) with c.vim plugin?

I've been setting up vim to us as my C/C++ IDE recently and have been really impressed with the c.vim plugin (found here). However, I'm working on a project that relies on a library that is not found in my system includes. When I compile manually, I just list this directory by passing g++ an include flag (e.g. g++ -I ~/lib/). Is there any way to set up c.vim to have this directory included when I use the "save and compile" ("\rc" by default) command?
Thanks!
I'd would just set $CXXFLAGS to -I~/lib, it's its job -- unless you are using the dumb makefile default configuration from mingw project, $CXXFLAGS will be recognized automagically, as long as your &makeprg still points to make and not directly to g++ or any other compiler (I don't know what c.vim does with this vim setting as I don't use it).
As I suspect you don't have any Makefile in your directory but just a monofile project, I'd prefer an extended modeline over project settings.

Resources