Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Beginner C question
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 12, 2020 4:08 pm    Post subject: Beginner C question Reply with quote

Hello everyone.

I've just completed my first C programm after doing nothing but the simple school lessons back in the days. Now fast forward some decades later I managed to write it based on my years of experience in (web) development. It runs and was developed on linux only.

As far as my google foo goes, there is no meaningfull port or automatic transfer for a linux C programm to Windows. Most ppl say, it is better to rewrite it. Since I managed to create it in C as a new language for myself and the programm could be usefull in windows too (it is a commandline client programm. No tui) I want to know if you have any good resources about "porting" the programm to windows.
_________________
My personal space
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 2264
Location: Frankfurt, Germany

PostPosted: Tue May 12, 2020 4:23 pm    Post subject: Reply with quote

Why do you think that your program won't run under Windows? As long as you don't use any Linux-specific libraries or system calls, you should be able to port your program to Windows with little effort.

Which compiler do you use under Windows? Visual Studio? Then look at links below to find out how to create and compile a C++ console app with Visual Studio:


Last edited by mike155 on Tue May 12, 2020 4:37 pm; edited 1 time in total
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 12, 2020 4:36 pm    Post subject: Reply with quote

mike155 wrote:
Why do you think that your program won't run under Windows?

Well, because I haven't done it yet and don't know how ;-)

mike155 wrote:
Then look at https://docs.microsoft.com/en-US/cpp/build/vscpp-step-2-build?view=vs-2019 to find out how to compile a C++ console app with Visual Studio.

On linux I did not use any IDE. I created a Makefile to build it.
I will use an IDE in the future since I can not remember everything and an IDE does help a lot, but for learning I think it is the best without it.

So, it is plain C (as far as I understand). Can I use VS for that, since the mentioned link writes about C++?
I starting from zero, there is no compiler or anything existing I can use.
EDIT: found it: https://docs.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019

EDIT: and mybe it can help to see the code I wrote: http://91.132.146.200:3000/Banana/selfpaste/src/master/client/c-client
This is not a promotion about the project I've created. I'm just learning and want to know how.
_________________
My personal space


Last edited by Banana on Tue May 12, 2020 4:42 pm; edited 1 time in total
Back to top
View user's profile Send private message
C5ace
Guru
Guru


Joined: 23 Dec 2013
Posts: 350
Location: Brisbane, Australia

PostPosted: Tue May 12, 2020 4:39 pm    Post subject: Reply with quote

mingw and CodeBlocks. Install both on Windows and recompile your code. You will have to change some linux headers to windows headers and IO functions.

http://www.codeblocks.org/downloads/26#windows

Have a look at os.h the #ifdef OS_LINUX and #ifdef OS_WIN32 in various .c and .h files in:

www.linux.c5ace.com/MXcleaner-1.0.0_Source.zip
_________________
Observation after 30 years working with computers:
All software has known and unknown bugs and vulnerabilities. Especially software written in complex, unstable and object oriented languages such as python, perl, C++, C#, Rust and the likes.
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 2264
Location: Frankfurt, Germany

PostPosted: Tue May 12, 2020 4:46 pm    Post subject: Reply with quote

Quote:
So, it is plain C (as far as I understand). Can I use VS for that, since the mentioned link writes about C++?

Well, Visual Studio is like GCC or CLANG. It supports C as well as C++. Visual Studio has a command line compiler, too - just in case you don't want to use the GUI. See: https://docs.microsoft.com/en-US/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 12, 2020 6:24 pm    Post subject: Reply with quote

does anyone know what is the difference between visual studio and visual studio code?

@mike155
Thank you for your suggestions, I'm gonna give it a try.

@C5ace
Thank you for mention codeBlocks, also gonna give it a try.
_________________
My personal space
Back to top
View user's profile Send private message
mno
Guru
Guru


Joined: 29 Dec 2003
Posts: 452
Location: Toronto, Canada

PostPosted: Tue May 12, 2020 6:33 pm    Post subject: Reply with quote

https://stackoverflow.com/questions/30527522/what-are-the-differences-between-visual-studio-code-and-visual-studio
_________________
"Hello and goodbye. As always." | You can't use   here?? | Unanswered
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Wed May 13, 2020 6:11 pm    Post subject: Reply with quote

thx for all the input.

I tried Visual Studio and was not pleased. Creating a simple c file is strange. Writing c++ is the preferred language, but not C. So it does make it not easier to do this in Visual Studio. Although I'm used to IntelliJ, I'm not convinced that this is the righ tool for me.

Tried codeblocks. It is simple and should do the job. I was able to write and run a simple c project within minutes. So I will stick to it for now.
_________________
My personal space
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 19, 2020 7:02 am    Post subject: Reply with quote

Little update on my journey:
Used CodeBlocks on windows with TDM-GCC and stuff. Gave up as the dependencies grew and not easy to manage (build curl with ssl on windows, doh). Looked into crossdev and mingw64 in Gentoo. Well I'm not convinced. I have the feeling that the amount of things to do for the result is too much.

I'm rethinking using c++ since Visual Studio slightly forces you to use this language.

Any other ideas are welcome.
_________________
My personal space
Back to top
View user's profile Send private message
Maf
Guru
Guru


Joined: 15 May 2005
Posts: 304

PostPosted: Tue May 19, 2020 7:50 am    Post subject: Reply with quote

@Banana: I'm not sure anymore what exactly your goal is, but if you want to build/run your little program on Windows: I wrote once a simple 3d game that I could build both on Gentoo and Windows. I used no IDE, just simple text editor. Let me know if that's your usecase; I might still find this old project on mine.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 19, 2020 12:24 pm    Post subject: Reply with quote

@Maf
My naiv thought was: Well I have a C program, why not run it on windows. Should be easy, right?
Well copy and paste a C program and build it on windows does not the trick. The dependencies, which are needed to build on windows too, are my pain in the ass.

Link to the code is in the first post
_________________
My personal space
Back to top
View user's profile Send private message
UlFie
Tux's lil' helper
Tux's lil' helper


Joined: 01 Nov 2011
Posts: 112
Location: Wuppertal

PostPosted: Tue May 19, 2020 3:36 pm    Post subject: Reply with quote

Maybe Cygwin is what you need.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18505

PostPosted: Tue May 19, 2020 4:02 pm    Post subject: Reply with quote

I've been using clang/gcc and vi, but I'm not trying to write for Windows.

From what I've read, it seems one solution is to pull out platform specific code and create "wrappers" so that the rest of your code is platform agnostic. Then during compile time, ifdefs help to provide the platform specific executable.

I used VS (community version a few years back) only very briefly, but I thought there were compile options to use C standards. I may be misremembering. It is also possible to write C and compile as C++.
_________________
The media sells it and you live the role.
Back to top
View user's profile Send private message
dmpogo
Advocate
Advocate


Joined: 02 Sep 2004
Posts: 2789
Location: Canada

PostPosted: Tue May 19, 2020 7:06 pm    Post subject: Reply with quote

There are two separate issues that seem intertwined in this discussion.

1) have windows environment in which you can compile C code.
2) Port your C code that runs on linux to windows

1) You have to do if you plan to develop under windows. That may come with whatever dependencies needed.
2) Hopefully, fairly straightforward, basically replacing libraries if you use linux specific ones.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 19, 2020 8:12 pm    Post subject: Reply with quote

dmpogo wrote:
1) have windows environment in which you can compile C code.

1) You have to do if you plan to develop under windows. That may come with whatever dependencies needed.

And of that "problem" I did not think of. You need all the dependencies in windows, too. Also you need to build them. and that it is where my intentions stopped. For example I use libcurl. You need to build it on windows. This requires openssl and perl etc. etc....
Handling all this with a combination of mingw, cygwin, mssys and whatever you need else was a aha moment which slowed my efforts doing it.

On Gentoo you already have those dependencies resolved so you don't think about them. On windows you need to create such an environment.

I see this as a learning experience and maybe I will try to start to learn c++ since this is a language which can written with linux and is favored on windows (VS brings the whole bag of stuff with it)
_________________
My personal space
Back to top
View user's profile Send private message
GDH-gentoo
Guru
Guru


Joined: 20 Jul 2019
Posts: 434
Location: South America

PostPosted: Tue May 19, 2020 9:22 pm    Post subject: Reply with quote

This is more a Windows question than a C question :)

dmpogo wrote:
There are two separate issues that seem intertwined in this discussion.

1) have windows environment in which you can compile C code.
2) Port your C code that runs on linux to Windows
Yeah. The language itself is not much of a problem here as long as it has adequate support by compilers or IDEs for Windows, i.e. issue #1. The important part here is the operating system's interfaces IMO, i.e. issue #2.

Banana wrote:
EDIT: and mybe it can help to see the code I wrote: http://91.132.146.200:3000/Banana/selfpaste/src/master/client/c-client
Let's see:
  • Stuff provided by <stdio.h>, <string.h>, and some of <time.h> is standard C, so any C compiler for Windows should generally support it fine.
  • Stuff provided <unistd.h> and <pwd.h> is only guaranteed to be present on OSes that adhere to the POSIX API, and MS Windows does not, or at least didn't use to. I don't know if this aspect has changed recently, or if compatibility layers are easily available.
  • Stuff provided by <argp.h> is GNU libc-specific. You won't have it even on Linux if the libc is musl, for example.
  • And as you already realized, to use stuff provided by <curl/curl.h> and <json-c/json.h> you need Windows ports of curl and json-c.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 19, 2020 9:38 pm    Post subject: Reply with quote

for argp.h I found argtable.h which works but needs a rewrite in the code I use. No dop in replacement. But got it working. For curl and json-c, well you know my pain.

[EDIT]
After writing this I did a search for the pre build libcurl. Well. Just scroll down to the bottom of https://curl.haxx.se/download.html :oops:

Now on to something for the json part...
_________________
My personal space
Back to top
View user's profile Send private message
UlFie
Tux's lil' helper
Tux's lil' helper


Joined: 01 Nov 2011
Posts: 112
Location: Wuppertal

PostPosted: Tue May 19, 2020 11:57 pm    Post subject: Reply with quote

I would once again like to suggest looking at Cygwin. Disclaimer: I am in no way affiliated with that project!

Cygwin is a bit like a (binary) Linux distribution, and there are packages with binaries of many libraries you know from Linux as well as the corresponding development packages (*-devel, as is common in Linux binary distributions).

Banana wrote:
You need all the dependencies in windows, too. Also you need to build them. [...] For example I use libcurl. You need to build it on windows. This requires openssl and perl etc. etc....

No, you don't need to build it with Cygwin, just install it and Cygwin's package manager should pull in all dependencies.

Banana wrote:
On Gentoo you already have those dependencies resolved so you don't think about them. On windows you need to create such an environment.

No, you only have them on Gentoo if you previously installed them or something that depends on them. Whilst libcurl may be part of what @system pulls in, you can't expect that an arbitrary library you decide to use in your code will be installed automagically, you have to emerge a suitable package. You can do more or less the same in Cygwin.

Banana wrote:
I see this as a learning experience and maybe I will try to start to learn c++ since this is a language which can written with linux and is favored on windows (VS brings the whole bag of stuff with it)

You should not expect C++ from VS to be in any way related to C++ as used in major Linux-related projects like KDE. Once upon a time, M$ didn't even have an IDE for its C/C++ compiler that ran under its infamous GUI, it was text mode only (similar to what ncurses makes possible). And writing a simple `Hello, world!' program and getting it compiled was almost impossible (and totally undocumented).

GDH-gentoo wrote:
  • Stuff provided by <argp.h> is GNU libc-specific. You won't have it even on Linux if the libc is musl, for example.
  • And as you already realized, to use stuff provided by <curl/curl.h> and <json-c/json.h> you need Windows ports of curl and json-c.

All that stuff is available in Cygwin (according to the package search function on the website)!

Banana wrote:
for argp.h I found argtable.h which works but needs a rewrite in the code I use. No dop in replacement. But got it working. For curl and json-c, well you know my pain.

After writing this I did a search for the pre build libcurl. Well. Just scroll down to the bottom of https://curl.haxx.se/download.html :oops:

Now on to something for the json part...

Once again, all that should be available in Cygwin, the package search function on the website even returns something for argp (so probably no need to change code)! Given that there are different compilers and tool chains, `prebuilt' can have a lot of meanings, I would go for something that uniformly provides all that is needed.
Back to top
View user's profile Send private message
niffel
n00b
n00b


Joined: 30 Jan 2020
Posts: 6

PostPosted: Wed May 20, 2020 12:27 am    Post subject: Reply with quote

If your end goal is just to be able to compile and run on a window CLI, and you're running window 10, I would take a look at WSL. I use Ubunto ( https://ubuntu.com/wsl ) but I've read that you can get Gentoo working as well.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Sun May 24, 2020 5:20 pm    Post subject: Reply with quote

@UlFie
Thanks for the detailed response. My learing journey continues.

Now I'm "stuck" with the dll "problem". I thought I can easily build a standalone exe file. Right now I managed to install cygwin. Added the dependencies. Updated my makefile and build it.
The resulting exe depends on some cygwin-*.dlls
As far as I know understand that it is how cygwin works. You can build it but it needs cygwin to run, does it?
_________________
My personal space
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 15623

PostPosted: Sun May 24, 2020 5:52 pm    Post subject: Reply with quote

If you use cygwin, yes. That is why some projects prefer mingw. Using mingw produces an executable with fewer dependencies, but you may need to do more porting work in your source code because mingw is much more minimal than cygwin.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Mon May 25, 2020 9:24 pm    Post subject: Reply with quote

small steps in my journey ;-)

I've managed to build it on windows with the help of tdm-gcc and the following bat file:
Code:
gcc -Lcurl/lib/ -Icurl/include -Iargtable/ -Lcjson/ -Icjson/ selfpaste-win.c argtable/argtable3.c cjson/cJSON.c -o selfpaste.exe -lcurl


curl and ssl is from here: https://github.com/curl/curl-for-win
argtable: https://www.argtable.org/
json: https://github.com/DaveGamble/cJSON

diretory listing to match the gcc parameters
Code:
argtable/
cjson/
curl/
makefile.bat
selfpaste-win.c


What I dont understand now that I still need libcrypto-1_1-x64, libcurl-x64 and libssl-1_1-x64.dll file to run my exe. Which I have, since they are included in thr curl-for-win packages.
Is there a way to include it into the exe? Making it a standalone file or do you think im heading into the wrong direction / don't understand it well enough?

Thx for the input so far and taking part in my learning experience :)
_________________
My personal space
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 15623

PostPosted: Tue May 26, 2020 1:04 am    Post subject: Reply with quote

Shared objects, or their Windows counterparts (DLLs) must be present for the program to run because the implementation of crypto, ssl, curl, etc. is in the shared object, not in your executable. From a technical perspective, yes, you could make your program standalone by using static libraries instead of shared libraries. However, before you do so, consider the license terms of the components you are using. It is relatively common for licenses to require different terms of compliance for statically linked versus shared linked code.
Back to top
View user's profile Send private message
Banana
Guru
Guru


Joined: 21 May 2004
Posts: 571
Location: Germany

PostPosted: Tue May 26, 2020 6:50 am    Post subject: Reply with quote

Thx Hu,

will look into it. I'm glad it is running now ;-)
_________________
My personal space
Back to top
View user's profile Send private message
proteusx
Apprentice
Apprentice


Joined: 21 Jan 2008
Posts: 293

PostPosted: Tue May 26, 2020 7:50 am    Post subject: Reply with quote

If this is of any use, this is an example of a console application that is written to compile in both Linux and Windows.
Here the Makefile that will compile the same C source for Linux and for Windows. All development was done in linux with vim, gnumake, gcc and Mingw32 under crossdev.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum